patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / message / i2o / i2o_core.c
1 /*
2  * Core I2O structure management 
3  * 
4  * (C) Copyright 1999-2002   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/additions:
17  *      Philipp Rumpf
18  *      Juha Sievänen <Juha.Sievanen@cs.Helsinki.FI>
19  *      Auvo Häkkinen <Auvo.Hakkinen@cs.Helsinki.FI>
20  *      Deepak Saxena <deepak@plexity.net>
21  *      Boji T Kannanthanam <boji.t.kannanthanam@intel.com>
22  *      Alan Cox <alan@redhat.com>:
23  *              Ported to Linux 2.5.
24  *      Markus Lidel <Markus.Lidel@shadowconnect.com>:
25  *              Minor fixes for 2.6.
26  * 
27  */
28
29 #include <linux/config.h>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/pci.h>
33
34 #include <linux/i2o.h>
35
36 #include <linux/errno.h>
37 #include <linux/init.h>
38 #include <linux/slab.h>
39 #include <linux/spinlock.h>
40 #include <linux/smp_lock.h>
41
42 #include <linux/bitops.h>
43 #include <linux/wait.h>
44 #include <linux/delay.h>
45 #include <linux/timer.h>
46 #include <linux/interrupt.h>
47 #include <linux/sched.h>
48 #include <asm/semaphore.h>
49 #include <linux/completion.h>
50 #include <linux/workqueue.h>
51
52 #include <asm/io.h>
53 #include <linux/reboot.h>
54 #ifdef CONFIG_MTRR
55 #include <asm/mtrr.h>
56 #endif // CONFIG_MTRR
57
58 #include "i2o_lan.h"
59
60 //#define DRIVERDEBUG
61
62 #ifdef DRIVERDEBUG
63 #define dprintk(s, args...) printk(s, ## args)
64 #else
65 #define dprintk(s, args...)
66 #endif
67
68 /* OSM table */
69 static struct i2o_handler *i2o_handlers[MAX_I2O_MODULES];
70
71 /* Controller list */
72 static struct i2o_controller *i2o_controllers[MAX_I2O_CONTROLLERS];
73 struct i2o_controller *i2o_controller_chain;
74 int i2o_num_controllers;
75
76 /* Initiator Context for Core message */
77 static int core_context;
78
79 /* Initialization && shutdown functions */
80 void i2o_sys_init(void);
81 static void i2o_sys_shutdown(void);
82 static int i2o_reset_controller(struct i2o_controller *);
83 static int i2o_reboot_event(struct notifier_block *, unsigned long , void *);
84 static int i2o_online_controller(struct i2o_controller *);
85 static int i2o_init_outbound_q(struct i2o_controller *);
86 static int i2o_post_outbound_messages(struct i2o_controller *);
87
88 /* Reply handler */
89 static void i2o_core_reply(struct i2o_handler *, struct i2o_controller *,
90                            struct i2o_message *);
91
92 /* Various helper functions */
93 static int i2o_lct_get(struct i2o_controller *);
94 static int i2o_lct_notify(struct i2o_controller *);
95 static int i2o_hrt_get(struct i2o_controller *);
96
97 static int i2o_build_sys_table(void);
98 static int i2o_systab_send(struct i2o_controller *c);
99
100 /* I2O core event handler */
101 static int i2o_core_evt(void *);
102 static int evt_pid;
103 static int evt_running;
104
105 /* Dynamic LCT update handler */
106 static int i2o_dyn_lct(void *);
107
108 void i2o_report_controller_unit(struct i2o_controller *, struct i2o_device *);
109
110 static void i2o_pci_dispose(struct i2o_controller *c);
111
112 /*
113  * I2O System Table.  Contains information about
114  * all the IOPs in the system.  Used to inform IOPs
115  * about each other's existence.
116  *
117  * sys_tbl_ver is the CurrentChangeIndicator that is
118  * used by IOPs to track changes.
119  */
120 static struct i2o_sys_tbl *sys_tbl;
121 static int sys_tbl_ind;
122 static int sys_tbl_len;
123
124 /*
125  * This spin lock is used to keep a device from being
126  * added and deleted concurrently across CPUs or interrupts.
127  * This can occur when a user creates a device and immediatelly
128  * deletes it before the new_dev_notify() handler is called.
129  */
130 static spinlock_t i2o_dev_lock = SPIN_LOCK_UNLOCKED;
131
132 /*
133  * Structures and definitions for synchronous message posting.
134  * See i2o_post_wait() for description.
135  */ 
136 struct i2o_post_wait_data
137 {
138         int *status;            /* Pointer to status block on caller stack */
139         int *complete;          /* Pointer to completion flag on caller stack */
140         u32 id;                 /* Unique identifier */
141         wait_queue_head_t *wq;  /* Wake up for caller (NULL for dead) */
142         struct i2o_post_wait_data *next;        /* Chain */
143         void *mem[2];           /* Memory blocks to recover on failure path */
144         dma_addr_t phys[2];     /* Physical address of blocks to recover */
145         u32 size[2];            /* Size of blocks to recover */
146 };
147
148 static struct i2o_post_wait_data *post_wait_queue;
149 static u32 post_wait_id;        // Unique ID for each post_wait
150 static spinlock_t post_wait_lock = SPIN_LOCK_UNLOCKED;
151 static void i2o_post_wait_complete(struct i2o_controller *, u32, int);
152
153 /* OSM descriptor handler */ 
154 static struct i2o_handler i2o_core_handler =
155 {
156         (void *)i2o_core_reply,
157         NULL,
158         NULL,
159         NULL,
160         "I2O core layer",
161         0,
162         I2O_CLASS_EXECUTIVE
163 };
164
165 /*
166  * Used when queueing a reply to be handled later
167  */
168  
169 struct reply_info
170 {
171         struct i2o_controller *iop;
172         u32 msg[MSG_FRAME_SIZE];
173 };
174 static struct reply_info evt_reply;
175 static struct reply_info events[I2O_EVT_Q_LEN];
176 static int evt_in;
177 static int evt_out;
178 static int evt_q_len;
179 #define MODINC(x,y) ((x) = ((x) + 1) % (y))
180
181 /*
182  * I2O configuration spinlock. This isnt a big deal for contention
183  * so we have one only
184  */
185
186 static DECLARE_MUTEX(i2o_configuration_lock);
187
188 /* 
189  * Event spinlock.  Used to keep event queue sane and from
190  * handling multiple events simultaneously.
191  */
192 static spinlock_t i2o_evt_lock = SPIN_LOCK_UNLOCKED;
193
194 /*
195  * Semaphore used to synchronize event handling thread with 
196  * interrupt handler.
197  */
198  
199 static DECLARE_MUTEX(evt_sem);
200 static DECLARE_COMPLETION(evt_dead);
201 static DECLARE_WAIT_QUEUE_HEAD(evt_wait);
202
203 static struct notifier_block i2o_reboot_notifier =
204 {
205         i2o_reboot_event,
206         NULL,
207         0
208 };
209
210 /*
211  *      Config options
212  */
213
214 static int verbose;
215
216 #if BITS_PER_LONG == 64
217 /**
218  *      i2o_context_list_add -  append an ptr to the context list and return a
219  *                              matching context id.
220  *      @ptr: pointer to add to the context list
221  *      @c: controller to which the context list belong
222  *      returns context id, which could be used in the transaction context
223  *      field.
224  *
225  *      Because the context field in I2O is only 32-bit large, on 64-bit the
226  *      pointer is to large to fit in the context field. The i2o_context_list
227  *      functiones map pointers to context fields.
228  */
229 u32 i2o_context_list_add(void *ptr, struct i2o_controller *c) {
230         u32 context = 1;
231         struct i2o_context_list_element **entry = &c->context_list;
232         struct i2o_context_list_element *element;
233         unsigned long flags;
234
235         spin_lock_irqsave(&c->context_list_lock, flags);
236         while(*entry && ((*entry)->flags & I2O_CONTEXT_LIST_USED)) {
237                 if((*entry)->context >= context)
238                         context = (*entry)->context + 1;
239                 entry = &((*entry)->next);
240         }
241
242         if(!*entry) {
243                 if(unlikely(!context)) {
244                         spin_unlock_irqrestore(&c->context_list_lock, flags);
245                         printk(KERN_EMERG "i2o_core: context list overflow\n");
246                         return 0;
247                 }
248
249                 element = kmalloc(sizeof(struct i2o_context_list_element), GFP_KERNEL);
250                 if(!element) {
251                         printk(KERN_EMERG "i2o_core: could not allocate memory for context list element\n");
252                         return 0;
253                 }
254                 element->context = context;
255                 element->next = NULL;
256                 *entry = element;
257         } else
258                 element = *entry;
259
260         element->ptr = ptr;
261         element->flags = I2O_CONTEXT_LIST_USED;
262
263         spin_unlock_irqrestore(&c->context_list_lock, flags);
264         dprintk(KERN_DEBUG "i2o_core: add context to list %p -> %d\n", ptr, context);
265         return context;
266 }
267
268 /**
269  *      i2o_context_list_remove - remove a ptr from the context list and return
270  *                                the matching context id.
271  *      @ptr: pointer to be removed from the context list
272  *      @c: controller to which the context list belong
273  *      returns context id, which could be used in the transaction context
274  *      field.
275  */
276 u32 i2o_context_list_remove(void *ptr, struct i2o_controller *c) {
277         struct i2o_context_list_element **entry = &c->context_list;
278         struct i2o_context_list_element *element;
279         u32 context;
280         unsigned long flags;
281
282         spin_lock_irqsave(&c->context_list_lock, flags);
283         while(*entry && ((*entry)->ptr != ptr))
284                 entry = &((*entry)->next);
285
286         if(unlikely(!*entry)) {
287                 spin_unlock_irqrestore(&c->context_list_lock, flags);
288                 printk(KERN_WARNING "i2o_core: could not remove nonexistent ptr %p\n", ptr);
289                 return 0;
290         }
291
292         element = *entry;
293
294         context = element->context;
295         element->ptr = NULL;
296         element->flags |= I2O_CONTEXT_LIST_DELETED;
297
298         spin_unlock_irqrestore(&c->context_list_lock, flags);
299         dprintk(KERN_DEBUG "i2o_core: markt as deleted in context list %p -> %d\n", ptr, context);
300         return context;
301 }
302
303 /**
304  *      i2o_context_list_get -  get a ptr from the context list and remove it
305  *                              from the list.
306  *      @context: context id to which the pointer belong
307  *      @c: controller to which the context list belong
308  *      returns pointer to the matching context id
309  */
310 void *i2o_context_list_get(u32 context, struct i2o_controller *c) {
311         struct i2o_context_list_element **entry = &c->context_list;
312         struct i2o_context_list_element *element;
313         void *ptr;
314         int count = 0;
315         unsigned long flags;
316
317         spin_lock_irqsave(&c->context_list_lock, flags);
318         while(*entry && ((*entry)->context != context)) {
319                 entry = &((*entry)->next);
320                 count ++;
321         }
322
323         if(unlikely(!*entry)) {
324                 spin_unlock_irqrestore(&c->context_list_lock, flags);
325                 printk(KERN_WARNING "i2o_core: context id %d not found\n", context);
326                 return NULL;
327         }
328
329         element = *entry;
330         ptr = element->ptr;
331         if(count >= I2O_CONTEXT_LIST_MIN_LENGTH) {
332                 *entry = (*entry)->next;
333                 kfree(element);
334         } else {
335                 element->ptr = NULL;
336                 element->flags &= !I2O_CONTEXT_LIST_USED;
337         }
338
339         spin_unlock_irqrestore(&c->context_list_lock, flags);
340         dprintk(KERN_DEBUG "i2o_core: get ptr from context list %d -> %p\n", context, ptr);
341         return ptr;
342 }
343 #endif
344
345 /*
346  * I2O Core reply handler
347  */
348 static void i2o_core_reply(struct i2o_handler *h, struct i2o_controller *c,
349                     struct i2o_message *m)
350 {
351         u32 *msg=(u32 *)m;
352         u32 status;
353         u32 context = msg[2];
354
355         if (msg[0] & MSG_FAIL) // Fail bit is set
356         {
357                 u32 *preserved_msg = (u32*)(c->mem_offset + msg[7]);
358
359                 i2o_report_status(KERN_INFO, "i2o_core", msg);
360                 i2o_dump_message(preserved_msg);
361
362                 /* If the failed request needs special treatment,
363                  * it should be done here. */
364
365                 /* Release the preserved msg by resubmitting it as a NOP */
366
367                 preserved_msg[0] = cpu_to_le32(THREE_WORD_MSG_SIZE | SGL_OFFSET_0);
368                 preserved_msg[1] = cpu_to_le32(I2O_CMD_UTIL_NOP << 24 | HOST_TID << 12 | 0);
369                 preserved_msg[2] = 0;
370                 i2o_post_message(c, msg[7]);
371
372                 /* If reply to i2o_post_wait failed, return causes a timeout */
373
374                 return;
375         }       
376
377 #ifdef DRIVERDEBUG
378         i2o_report_status(KERN_INFO, "i2o_core", msg);
379 #endif
380
381         if(msg[2]&0x80000000)   // Post wait message
382         {
383                 if (msg[4] >> 24)
384                         status = (msg[4] & 0xFFFF);
385                 else
386                         status = I2O_POST_WAIT_OK;
387         
388                 i2o_post_wait_complete(c, context, status);
389                 return;
390         }
391
392         if(m->function == I2O_CMD_UTIL_EVT_REGISTER)
393         {
394                 memcpy(events[evt_in].msg, msg, (msg[0]>>16)<<2);
395                 events[evt_in].iop = c;
396
397                 spin_lock(&i2o_evt_lock);
398                 MODINC(evt_in, I2O_EVT_Q_LEN);
399                 if(evt_q_len == I2O_EVT_Q_LEN)
400                         MODINC(evt_out, I2O_EVT_Q_LEN);
401                 else
402                         evt_q_len++;
403                 spin_unlock(&i2o_evt_lock);
404
405                 up(&evt_sem);
406                 wake_up_interruptible(&evt_wait);
407                 return;
408         }
409
410         if(m->function == I2O_CMD_LCT_NOTIFY)
411         {
412                 up(&c->lct_sem);
413                 return;
414         }
415
416         /*
417          * If this happens, we want to dump the message to the syslog so
418          * it can be sent back to the card manufacturer by the end user
419          * to aid in debugging.
420          * 
421          */
422         printk(KERN_WARNING "%s: Unsolicited message reply sent to core!"
423                         "Message dumped to syslog\n", 
424                         c->name);
425         i2o_dump_message(msg);
426
427         return;
428 }
429
430 /**
431  *      i2o_install_handler - install a message handler
432  *      @h: Handler structure
433  *
434  *      Install an I2O handler - these handle the asynchronous messaging
435  *      from the card once it has initialised. If the table of handlers is
436  *      full then -ENOSPC is returned. On a success 0 is returned and the
437  *      context field is set by the function. The structure is part of the
438  *      system from this time onwards. It must not be freed until it has
439  *      been uninstalled
440  */
441  
442 int i2o_install_handler(struct i2o_handler *h)
443 {
444         int i;
445         down(&i2o_configuration_lock);
446         for(i=0;i<MAX_I2O_MODULES;i++)
447         {
448                 if(i2o_handlers[i]==NULL)
449                 {
450                         h->context = i;
451                         i2o_handlers[i]=h;
452                         up(&i2o_configuration_lock);
453                         return 0;
454                 }
455         }
456         up(&i2o_configuration_lock);
457         return -ENOSPC;
458 }
459
460 /**
461  *      i2o_remove_handler - remove an i2o message handler
462  *      @h: handler
463  *
464  *      Remove a message handler previously installed with i2o_install_handler.
465  *      After this function returns the handler object can be freed or re-used
466  */
467  
468 int i2o_remove_handler(struct i2o_handler *h)
469 {
470         i2o_handlers[h->context]=NULL;
471         return 0;
472 }
473         
474
475 /*
476  *      Each I2O controller has a chain of devices on it.
477  * Each device has a pointer to its LCT entry to be used
478  * for fun purposes.
479  */
480
481 /**
482  *      i2o_install_device      -       attach a device to a controller
483  *      @c: controller
484  *      @d: device
485  *      
486  *      Add a new device to an i2o controller. This can be called from
487  *      non interrupt contexts only. It adds the device and marks it as
488  *      unclaimed. The device memory becomes part of the kernel and must
489  *      be uninstalled before being freed or reused. Zero is returned
490  *      on success.
491  */
492  
493 int i2o_install_device(struct i2o_controller *c, struct i2o_device *d)
494 {
495         int i;
496
497         down(&i2o_configuration_lock);
498         d->controller=c;
499         d->owner=NULL;
500         d->next=c->devices;
501         d->prev=NULL;
502         if (c->devices != NULL)
503                 c->devices->prev=d;
504         c->devices=d;
505         *d->dev_name = 0;
506
507         for(i = 0; i < I2O_MAX_MANAGERS; i++)
508                 d->managers[i] = NULL;
509
510         up(&i2o_configuration_lock);
511         return 0;
512 }
513
514 /* we need this version to call out of i2o_delete_controller */
515
516 int __i2o_delete_device(struct i2o_device *d)
517 {
518         struct i2o_device **p;
519         int i;
520
521         p=&(d->controller->devices);
522
523         /*
524          *      Hey we have a driver!
525          * Check to see if the driver wants us to notify it of 
526          * device deletion. If it doesn't we assume that it
527          * is unsafe to delete a device with an owner and 
528          * fail.
529          */
530         if(d->owner)
531         {
532                 if(d->owner->dev_del_notify)
533                 {
534                         dprintk(KERN_INFO "Device has owner, notifying\n");
535                         d->owner->dev_del_notify(d->controller, d);
536                         if(d->owner)
537                         {
538                                 printk(KERN_WARNING 
539                                         "Driver \"%s\" did not release device!\n", d->owner->name);
540                                 return -EBUSY;
541                         }
542                 }
543                 else
544                         return -EBUSY;
545         }
546
547         /*
548          * Tell any other users who are talking to this device
549          * that it's going away.  We assume that everything works.
550          */
551         for(i=0; i < I2O_MAX_MANAGERS; i++)
552         {
553                 if(d->managers[i] && d->managers[i]->dev_del_notify)
554                         d->managers[i]->dev_del_notify(d->controller, d);
555         }
556                                 
557         while(*p!=NULL)
558         {
559                 if(*p==d)
560                 {
561                         /*
562                          *      Destroy
563                          */
564                         *p=d->next;
565                         kfree(d);
566                         return 0;
567                 }
568                 p=&((*p)->next);
569         }
570         printk(KERN_ERR "i2o_delete_device: passed invalid device.\n");
571         return -EINVAL;
572 }
573
574 /**
575  *      i2o_delete_device       -       remove an i2o device
576  *      @d: device to remove
577  *
578  *      This function unhooks a device from a controller. The device
579  *      will not be unhooked if it has an owner who does not wish to free
580  *      it, or if the owner lacks a dev_del_notify function. In that case
581  *      -EBUSY is returned. On success 0 is returned. Other errors cause
582  *      negative errno values to be returned
583  */
584  
585 int i2o_delete_device(struct i2o_device *d)
586 {
587         int ret;
588
589         down(&i2o_configuration_lock);
590
591         /*
592          *      Seek, locate
593          */
594
595         ret = __i2o_delete_device(d);
596
597         up(&i2o_configuration_lock);
598
599         return ret;
600 }
601
602 /**
603  *      i2o_install_controller  -       attach a controller
604  *      @c: controller
605  *      
606  *      Add a new controller to the i2o layer. This can be called from
607  *      non interrupt contexts only. It adds the controller and marks it as
608  *      unused with no devices. If the tables are full or memory allocations
609  *      fail then a negative errno code is returned. On success zero is
610  *      returned and the controller is bound to the system. The structure
611  *      must not be freed or reused until being uninstalled.
612  */
613  
614 int i2o_install_controller(struct i2o_controller *c)
615 {
616         int i;
617         down(&i2o_configuration_lock);
618         for(i=0;i<MAX_I2O_CONTROLLERS;i++)
619         {
620                 if(i2o_controllers[i]==NULL)
621                 {
622                         c->dlct = (i2o_lct*)pci_alloc_consistent(c->pdev, 8192, &c->dlct_phys);
623                         if(c->dlct==NULL)
624                         {
625                                 up(&i2o_configuration_lock);
626                                 return -ENOMEM;
627                         }
628                         i2o_controllers[i]=c;
629                         c->devices = NULL;
630                         c->next=i2o_controller_chain;
631                         i2o_controller_chain=c;
632                         c->unit = i;
633                         c->page_frame = NULL;
634                         c->hrt = NULL;
635                         c->hrt_len = 0;
636                         c->lct = NULL;
637                         c->status_block = NULL;
638                         sprintf(c->name, "i2o/iop%d", i);
639                         i2o_num_controllers++;
640                         init_MUTEX_LOCKED(&c->lct_sem);
641                         up(&i2o_configuration_lock);
642                         return 0;
643                 }
644         }
645         printk(KERN_ERR "No free i2o controller slots.\n");
646         up(&i2o_configuration_lock);
647         return -EBUSY;
648 }
649
650 /**
651  *      i2o_delete_controller   - delete a controller
652  *      @c: controller
653  *      
654  *      Remove an i2o controller from the system. If the controller or its
655  *      devices are busy then -EBUSY is returned. On a failure a negative
656  *      errno code is returned. On success zero is returned.
657  */
658   
659 int i2o_delete_controller(struct i2o_controller *c)
660 {
661         struct i2o_controller **p;
662         int users;
663         char name[16];
664         int stat;
665
666         dprintk(KERN_INFO "Deleting controller %s\n", c->name);
667
668         /*
669          * Clear event registration as this can cause weird behavior
670          */
671         if(c->status_block->iop_state == ADAPTER_STATE_OPERATIONAL)
672                 i2o_event_register(c, core_context, 0, 0, 0);
673
674         down(&i2o_configuration_lock);
675         if((users=atomic_read(&c->users)))
676         {
677                 dprintk(KERN_INFO "I2O: %d users for controller %s\n", users,
678                         c->name);
679                 up(&i2o_configuration_lock);
680                 return -EBUSY;
681         }
682         while(c->devices)
683         {
684                 if(__i2o_delete_device(c->devices)<0)
685                 {
686                         /* Shouldnt happen */
687                         I2O_IRQ_WRITE32(c, 0xFFFFFFFF);
688                         c->enabled = 0;
689                         up(&i2o_configuration_lock);
690                         return -EBUSY;
691                 }
692         }
693
694         /*
695          * If this is shutdown time, the thread's already been killed
696          */
697         if(c->lct_running) {
698                 stat = kill_proc(c->lct_pid, SIGKILL, 1);
699                 if(!stat) {
700                         int count = 10 * 100;
701                         while(c->lct_running && --count) {
702                                 current->state = TASK_INTERRUPTIBLE;
703                                 schedule_timeout(1);
704                         }
705                 
706                         if(!count)
707                                 printk(KERN_ERR 
708                                         "%s: LCT thread still running!\n", 
709                                         c->name);
710                 }
711         }
712
713         p=&i2o_controller_chain;
714
715         while(*p)
716         {
717                 if(*p==c)
718                 {
719                         /* Ask the IOP to switch to RESET state */
720                         i2o_reset_controller(c);
721
722                         /* Release IRQ */
723                         i2o_pci_dispose(c);
724
725                         *p=c->next;
726                         up(&i2o_configuration_lock);
727
728                         if(c->page_frame)
729                         {
730                                 pci_unmap_single(c->pdev, c->page_frame_map, MSG_POOL_SIZE, PCI_DMA_FROMDEVICE);
731                                 kfree(c->page_frame);
732                         }
733                         if(c->hrt)
734                                 pci_free_consistent(c->pdev, c->hrt_len, c->hrt, c->hrt_phys);
735                         if(c->lct)
736                                 pci_free_consistent(c->pdev, c->lct->table_size << 2, c->lct, c->lct_phys);
737                         if(c->status_block)
738                                 pci_free_consistent(c->pdev, sizeof(i2o_status_block), c->status_block, c->status_block_phys);
739                         if(c->dlct)
740                                 pci_free_consistent(c->pdev, 8192, c->dlct, c->dlct_phys);
741
742                         i2o_controllers[c->unit]=NULL;
743                         memcpy(name, c->name, strlen(c->name)+1);
744                         kfree(c);
745                         dprintk(KERN_INFO "%s: Deleted from controller chain.\n", name);
746                         
747                         i2o_num_controllers--;
748                         return 0;
749                 }
750                 p=&((*p)->next);
751         }
752         up(&i2o_configuration_lock);
753         printk(KERN_ERR "i2o_delete_controller: bad pointer!\n");
754         return -ENOENT;
755 }
756
757 /**
758  *      i2o_unlock_controller   -       unlock a controller
759  *      @c: controller to unlock
760  *
761  *      Take a lock on an i2o controller. This prevents it being deleted.
762  *      i2o controllers are not refcounted so a deletion of an in use device
763  *      will fail, not take affect on the last dereference.
764  */
765  
766 void i2o_unlock_controller(struct i2o_controller *c)
767 {
768         atomic_dec(&c->users);
769 }
770
771 /**
772  *      i2o_find_controller - return a locked controller
773  *      @n: controller number
774  *
775  *      Returns a pointer to the controller object. The controller is locked
776  *      on return. NULL is returned if the controller is not found.
777  */
778  
779 struct i2o_controller *i2o_find_controller(int n)
780 {
781         struct i2o_controller *c;
782         
783         if(n<0 || n>=MAX_I2O_CONTROLLERS)
784                 return NULL;
785         
786         down(&i2o_configuration_lock);
787         c=i2o_controllers[n];
788         if(c!=NULL)
789                 atomic_inc(&c->users);
790         up(&i2o_configuration_lock);
791         return c;
792 }
793
794 /**
795  *      i2o_issue_claim - claim or release a device
796  *      @cmd: command
797  *      @c: controller to claim for
798  *      @tid: i2o task id
799  *      @type: type of claim
800  *
801  *      Issue I2O UTIL_CLAIM and UTIL_RELEASE messages. The message to be sent
802  *      is set by cmd. The tid is the task id of the object to claim and the
803  *      type is the claim type (see the i2o standard)
804  *
805  *      Zero is returned on success.
806  */
807  
808 static int i2o_issue_claim(u32 cmd, struct i2o_controller *c, int tid, u32 type)
809 {
810         u32 msg[5];
811
812         msg[0] = FIVE_WORD_MSG_SIZE | SGL_OFFSET_0;
813         msg[1] = cmd << 24 | HOST_TID<<12 | tid;
814         msg[3] = 0;
815         msg[4] = type;
816         
817         return i2o_post_wait(c, msg, sizeof(msg), 60);
818 }
819
820 /*
821  *      i2o_claim_device - claim a device for use by an OSM
822  *      @d: device to claim
823  *      @h: handler for this device
824  *
825  *      Do the leg work to assign a device to a given OSM on Linux. The
826  *      kernel updates the internal handler data for the device and then
827  *      performs an I2O claim for the device, attempting to claim the
828  *      device as primary. If the attempt fails a negative errno code
829  *      is returned. On success zero is returned.
830  */
831  
832 int i2o_claim_device(struct i2o_device *d, struct i2o_handler *h)
833 {
834         down(&i2o_configuration_lock);
835         if (d->owner) {
836                 printk(KERN_INFO "Device claim called, but dev already owned by %s!",
837                        h->name);
838                 up(&i2o_configuration_lock);
839                 return -EBUSY;
840         }
841         d->owner=h;
842
843         if(i2o_issue_claim(I2O_CMD_UTIL_CLAIM ,d->controller,d->lct_data.tid, 
844                            I2O_CLAIM_PRIMARY))
845         {
846                 d->owner = NULL;
847                 return -EBUSY;
848         }
849         up(&i2o_configuration_lock);
850         return 0;
851 }
852
853 /**
854  *      i2o_release_device - release a device that the OSM is using
855  *      @d: device to claim
856  *      @h: handler for this device
857  *
858  *      Drop a claim by an OSM on a given I2O device. The handler is cleared
859  *      and 0 is returned on success.
860  *
861  *      AC - some devices seem to want to refuse an unclaim until they have
862  *      finished internal processing. It makes sense since you don't want a
863  *      new device to go reconfiguring the entire system until you are done.
864  *      Thus we are prepared to wait briefly.
865  */
866
867 int i2o_release_device(struct i2o_device *d, struct i2o_handler *h)
868 {
869         int err = 0;
870         int tries;
871
872         down(&i2o_configuration_lock);
873         if (d->owner != h) {
874                 printk(KERN_INFO "Claim release called, but not owned by %s!\n",
875                        h->name);
876                 up(&i2o_configuration_lock);
877                 return -ENOENT;
878         }       
879
880         for(tries=0;tries<10;tries++)
881         {
882                 d->owner = NULL;
883
884                 /*
885                  *      If the controller takes a nonblocking approach to
886                  *      releases we have to sleep/poll for a few times.
887                  */
888                  
889                 if((err=i2o_issue_claim(I2O_CMD_UTIL_RELEASE, d->controller, d->lct_data.tid, I2O_CLAIM_PRIMARY)) )
890                 {
891                         err = -ENXIO;
892                         current->state = TASK_UNINTERRUPTIBLE;
893                         schedule_timeout(HZ);
894                 }
895                 else
896                 {
897                         err=0;
898                         break;
899                 }
900         }
901         up(&i2o_configuration_lock);
902         return err;
903 }
904
905 /**
906  *      i2o_device_notify_on    -       Enable deletion notifiers
907  *      @d: device for notification
908  *      @h: handler to install
909  *
910  *      Called by OSMs to let the core know that they want to be
911  *      notified if the given device is deleted from the system.
912  */
913
914 int i2o_device_notify_on(struct i2o_device *d, struct i2o_handler *h)
915 {
916         int i;
917
918         if(d->num_managers == I2O_MAX_MANAGERS)
919                 return -ENOSPC;
920
921         for(i = 0; i < I2O_MAX_MANAGERS; i++)
922         {
923                 if(!d->managers[i])
924                 {
925                         d->managers[i] = h;
926                         break;
927                 }
928         }
929         
930         d->num_managers++;
931         
932         return 0;
933 }
934
935 /**
936  *      i2o_device_notify_off   -       Remove deletion notifiers
937  *      @d: device for notification
938  *      @h: handler to remove
939  *
940  * Called by OSMs to let the core know that they no longer
941  * are interested in the fate of the given device.
942  */
943 int i2o_device_notify_off(struct i2o_device *d, struct i2o_handler *h)
944 {
945         int i;
946
947         for(i=0; i < I2O_MAX_MANAGERS; i++)
948         {
949                 if(d->managers[i] == h)
950                 {
951                         d->managers[i] = NULL;
952                         d->num_managers--;
953                         return 0;
954                 }
955         }
956
957         return -ENOENT;
958 }
959
960 /**
961  *      i2o_event_register      -       register interest in an event
962  *      @c: Controller to register interest with
963  *      @tid: I2O task id
964  *      @init_context: initiator context to use with this notifier
965  *      @tr_context: transaction context to use with this notifier
966  *      @evt_mask: mask of events
967  *
968  *      Create and posts an event registration message to the task. No reply
969  *      is waited for, or expected. Errors in posting will be reported.
970  */
971  
972 int i2o_event_register(struct i2o_controller *c, u32 tid, 
973                 u32 init_context, u32 tr_context, u32 evt_mask)
974 {
975         u32 msg[5];     // Not performance critical, so we just 
976                         // i2o_post_this it instead of building it
977                         // in IOP memory
978         
979         msg[0] = FIVE_WORD_MSG_SIZE|SGL_OFFSET_0;
980         msg[1] = I2O_CMD_UTIL_EVT_REGISTER<<24 | HOST_TID<<12 | tid;
981         msg[2] = init_context;
982         msg[3] = tr_context;
983         msg[4] = evt_mask;
984
985         return i2o_post_this(c, msg, sizeof(msg));
986 }
987
988 /*
989  *      i2o_event_ack   -       acknowledge an event
990  *      @c: controller 
991  *      @msg: pointer to the UTIL_EVENT_REGISTER reply we received
992  *
993  *      We just take a pointer to the original UTIL_EVENT_REGISTER reply
994  *      message and change the function code since that's what spec
995  *      describes an EventAck message looking like.
996  */
997  
998 int i2o_event_ack(struct i2o_controller *c, u32 *msg)
999 {
1000         struct i2o_message *m = (struct i2o_message *)msg;
1001
1002         m->function = I2O_CMD_UTIL_EVT_ACK;
1003
1004         return i2o_post_wait(c, msg, m->size * 4, 2);
1005 }
1006
1007 /*
1008  * Core event handler.  Runs as a separate thread and is woken
1009  * up whenever there is an Executive class event.
1010  */
1011 static int i2o_core_evt(void *reply_data)
1012 {
1013         struct reply_info *reply = (struct reply_info *) reply_data;
1014         u32 *msg = reply->msg;
1015         struct i2o_controller *c = NULL;
1016         unsigned long flags;
1017
1018         daemonize("i2oevtd");
1019         allow_signal(SIGKILL);
1020
1021         evt_running = 1;
1022
1023         while(1)
1024         {
1025                 if(down_interruptible(&evt_sem))
1026                 {
1027                         dprintk(KERN_INFO "I2O event thread dead\n");
1028                         printk("exiting...");
1029                         evt_running = 0;
1030                         complete_and_exit(&evt_dead, 0);
1031                 }
1032
1033                 /* 
1034                  * Copy the data out of the queue so that we don't have to lock
1035                  * around the whole function and just around the qlen update
1036                  */
1037                 spin_lock_irqsave(&i2o_evt_lock, flags);
1038                 memcpy(reply, &events[evt_out], sizeof(struct reply_info));
1039                 MODINC(evt_out, I2O_EVT_Q_LEN);
1040                 evt_q_len--;
1041                 spin_unlock_irqrestore(&i2o_evt_lock, flags);
1042         
1043                 c = reply->iop;
1044                 dprintk(KERN_INFO "I2O IRTOS EVENT: iop%d, event %#10x\n", c->unit, msg[4]);
1045
1046                 /* 
1047                  * We do not attempt to delete/quiesce/etc. the controller if
1048                  * some sort of error indidication occurs.  We may want to do
1049                  * so in the future, but for now we just let the user deal with 
1050                  * it.  One reason for this is that what to do with an error
1051                  * or when to send what ærror is not really agreed on, so
1052                  * we get errors that may not be fatal but just look like they
1053                  * are...so let the user deal with it.
1054                  */
1055                 switch(msg[4])
1056                 {
1057                         case I2O_EVT_IND_EXEC_RESOURCE_LIMITS:
1058                                 printk(KERN_ERR "%s: Out of resources\n", c->name);
1059                                 break;
1060
1061                         case I2O_EVT_IND_EXEC_POWER_FAIL:
1062                                 printk(KERN_ERR "%s: Power failure\n", c->name);
1063                                 break;
1064
1065                         case I2O_EVT_IND_EXEC_HW_FAIL:
1066                         {
1067                                 char *fail[] = 
1068                                         { 
1069                                                 "Unknown Error",
1070                                                 "Power Lost",
1071                                                 "Code Violation",
1072                                                 "Parity Error",
1073                                                 "Code Execution Exception",
1074                                                 "Watchdog Timer Expired" 
1075                                         };
1076
1077                                 if(msg[5] <= 6)
1078                                         printk(KERN_ERR "%s: Hardware Failure: %s\n", 
1079                                                 c->name, fail[msg[5]]);
1080                                 else
1081                                         printk(KERN_ERR "%s: Unknown Hardware Failure\n", c->name);
1082
1083                                 break;
1084                         }
1085
1086                         /*
1087                          * New device created
1088                          * - Create a new i2o_device entry
1089                          * - Inform all interested drivers about this device's existence
1090                          */
1091                         case I2O_EVT_IND_EXEC_NEW_LCT_ENTRY:
1092                         {
1093                                 struct i2o_device *d = (struct i2o_device *)
1094                                         kmalloc(sizeof(struct i2o_device), GFP_KERNEL);
1095                                 int i;
1096
1097                                 if (d == NULL) {
1098                                         printk(KERN_EMERG "i2oevtd: out of memory\n");
1099                                         break;
1100                                 }
1101                                 memcpy(&d->lct_data, &msg[5], sizeof(i2o_lct_entry));
1102         
1103                                 d->next = NULL;
1104                                 d->controller = c;
1105                                 d->flags = 0;
1106         
1107                                 i2o_report_controller_unit(c, d);
1108                                 i2o_install_device(c,d);
1109         
1110                                 for(i = 0; i < MAX_I2O_MODULES; i++)
1111                                 {
1112                                         if(i2o_handlers[i] && 
1113                                                 i2o_handlers[i]->new_dev_notify &&
1114                                                 (i2o_handlers[i]->class&d->lct_data.class_id))
1115                                                 {
1116                                                 spin_lock(&i2o_dev_lock);
1117                                                 i2o_handlers[i]->new_dev_notify(c,d);
1118                                                 spin_unlock(&i2o_dev_lock);
1119                                                 }
1120                                 }
1121                         
1122                                 break;
1123                         }
1124         
1125                         /*
1126                          * LCT entry for a device has been modified, so update it
1127                          * internally.
1128                          */
1129                         case I2O_EVT_IND_EXEC_MODIFIED_LCT:
1130                         {
1131                                 struct i2o_device *d;
1132                                 i2o_lct_entry *new_lct = (i2o_lct_entry *)&msg[5];
1133
1134                                 for(d = c->devices; d; d = d->next)
1135                                 {
1136                                         if(d->lct_data.tid == new_lct->tid)
1137                                         {
1138                                                 memcpy(&d->lct_data, new_lct, sizeof(i2o_lct_entry));
1139                                                 break;
1140                                         }
1141                                 }
1142                                 break;
1143                         }
1144         
1145                         case I2O_EVT_IND_CONFIGURATION_FLAG:
1146                                 printk(KERN_WARNING "%s requires user configuration\n", c->name);
1147                                 break;
1148         
1149                         case I2O_EVT_IND_GENERAL_WARNING:
1150                                 printk(KERN_WARNING "%s: Warning notification received!"
1151                                         "Check configuration for errors!\n", c->name);
1152                                 break;
1153                                 
1154                         case I2O_EVT_IND_EVT_MASK_MODIFIED:
1155                                 /* Well I guess that was us hey .. */
1156                                 break;
1157                                         
1158                         default:
1159                                 printk(KERN_WARNING "%s: No handler for event (0x%08x)\n", c->name, msg[4]);
1160                                 break;
1161                 }
1162         }
1163
1164         return 0;
1165 }
1166
1167 /*
1168  * Dynamic LCT update.  This compares the LCT with the currently
1169  * installed devices to check for device deletions..this needed b/c there
1170  * is no DELETED_LCT_ENTRY EventIndicator for the Executive class so
1171  * we can't just have the event handler do this...annoying
1172  *
1173  * This is a hole in the spec that will hopefully be fixed someday.
1174  */
1175 static int i2o_dyn_lct(void *foo)
1176 {
1177         struct i2o_controller *c = (struct i2o_controller *)foo;
1178         struct i2o_device *d = NULL;
1179         struct i2o_device *d1 = NULL;
1180         int i = 0;
1181         int found = 0;
1182         int entries;
1183         void *tmp;
1184
1185         daemonize("iop%d_lctd", c->unit);
1186         allow_signal(SIGKILL);
1187
1188         c->lct_running = 1;
1189
1190         while(1)
1191         {
1192                 down_interruptible(&c->lct_sem);
1193                 if(signal_pending(current))
1194                 {
1195                         dprintk(KERN_ERR "%s: LCT thread dead\n", c->name);
1196                         c->lct_running = 0;
1197                         return 0;
1198                 }
1199
1200                 entries = c->dlct->table_size;
1201                 entries -= 3;
1202                 entries /= 9;
1203
1204                 dprintk(KERN_INFO "%s: Dynamic LCT Update\n",c->name);
1205                 dprintk(KERN_INFO "%s: Dynamic LCT contains %d entries\n", c->name, entries);
1206
1207                 if(!entries)
1208                 {
1209                         printk(KERN_INFO "%s: Empty LCT???\n", c->name);
1210                         continue;
1211                 }
1212
1213                 /*
1214                  * Loop through all the devices on the IOP looking for their
1215                  * LCT data in the LCT.  We assume that TIDs are not repeated.
1216                  * as that is the only way to really tell.  It's been confirmed
1217                  * by the IRTOS vendor(s?) that TIDs are not reused until they 
1218                  * wrap arround(4096), and I doubt a system will up long enough
1219                  * to create/delete that many devices.
1220                  */
1221                 for(d = c->devices; d; )
1222                 {
1223                         found = 0;
1224                         d1 = d->next;
1225                         
1226                         for(i = 0; i < entries; i++) 
1227                         { 
1228                                 if(d->lct_data.tid == c->dlct->lct_entry[i].tid) 
1229                                 { 
1230                                         found = 1; 
1231                                         break; 
1232                                 } 
1233                         } 
1234                         if(!found) 
1235                         {
1236                                 dprintk(KERN_INFO "i2o_core: Deleted device!\n"); 
1237                                 spin_lock(&i2o_dev_lock);
1238                                 i2o_delete_device(d); 
1239                                 spin_unlock(&i2o_dev_lock);
1240                         } 
1241                         d = d1; 
1242                 }
1243
1244                 /* 
1245                  * Tell LCT to renotify us next time there is a change
1246                  */
1247                 i2o_lct_notify(c);
1248
1249                 /*
1250                  * Copy new LCT into public LCT
1251                  *
1252                  * Possible race if someone is reading LCT while  we are copying 
1253                  * over it. If this happens, we'll fix it then. but I doubt that
1254                  * the LCT will get updated often enough or will get read by
1255                  * a user often enough to worry.
1256                  */
1257                 if(c->lct->table_size < c->dlct->table_size)
1258                 {
1259                         dma_addr_t phys;
1260                         tmp = c->lct;
1261                         c->lct = pci_alloc_consistent(c->pdev, c->dlct->table_size<<2, &phys);
1262                         if(!c->lct)
1263                         {
1264                                 printk(KERN_ERR "%s: No memory for LCT!\n", c->name);
1265                                 c->lct = tmp;
1266                                 continue;
1267                         }
1268                         pci_free_consistent(tmp, c->lct->table_size << 2, c->lct, c->lct_phys);
1269                         c->lct_phys = phys;
1270                 }
1271                 memcpy(c->lct, c->dlct, c->dlct->table_size<<2);
1272         }
1273
1274         return 0;
1275 }
1276
1277 /**
1278  *      i2o_run_queue   -       process pending events on a controller
1279  *      @c: controller to process
1280  *
1281  *      This is called by the bus specific driver layer when an interrupt
1282  *      or poll of this card interface is desired.
1283  */
1284  
1285 void i2o_run_queue(struct i2o_controller *c)
1286 {
1287         struct i2o_message *m;
1288         u32 mv;
1289         u32 *msg;
1290
1291         /*
1292          * Old 960 steppings had a bug in the I2O unit that caused
1293          * the queue to appear empty when it wasn't.
1294          */
1295         if((mv=I2O_REPLY_READ32(c))==0xFFFFFFFF)
1296                 mv=I2O_REPLY_READ32(c);
1297
1298         while(mv!=0xFFFFFFFF)
1299         {
1300                 struct i2o_handler *i;
1301                 /* Map the message from the page frame map to kernel virtual */
1302                 /* m=(struct i2o_message *)(mv - (unsigned long)c->page_frame_map + (unsigned long)c->page_frame); */
1303                 m=(struct i2o_message *)bus_to_virt(mv);
1304                 msg=(u32*)m;
1305
1306                 /*
1307                  *      Ensure this message is seen coherently but cachably by
1308                  *      the processor 
1309                  */
1310
1311                 pci_dma_sync_single_for_cpu(c->pdev, c->page_frame_map, MSG_FRAME_SIZE, PCI_DMA_FROMDEVICE);
1312         
1313                 /*
1314                  *      Despatch it
1315                  */
1316
1317                 i=i2o_handlers[m->initiator_context&(MAX_I2O_MODULES-1)];
1318                 if(i && i->reply)
1319                         i->reply(i,c,m);
1320                 else
1321                 {
1322                         printk(KERN_WARNING "I2O: Spurious reply to handler %d\n", 
1323                                 m->initiator_context&(MAX_I2O_MODULES-1));
1324                 }       
1325                 i2o_flush_reply(c,mv);
1326                 mb();
1327
1328                 /* That 960 bug again... */     
1329                 if((mv=I2O_REPLY_READ32(c))==0xFFFFFFFF)
1330                         mv=I2O_REPLY_READ32(c);
1331         }               
1332 }
1333
1334
1335 /**
1336  *      i2o_get_class_name -    do i2o class name lookup
1337  *      @class: class number
1338  *
1339  *      Return a descriptive string for an i2o class
1340  */
1341  
1342 const char *i2o_get_class_name(int class)
1343 {
1344         int idx = 16;
1345         static char *i2o_class_name[] = {
1346                 "Executive",
1347                 "Device Driver Module",
1348                 "Block Device",
1349                 "Tape Device",
1350                 "LAN Interface",
1351                 "WAN Interface",
1352                 "Fibre Channel Port",
1353                 "Fibre Channel Device",
1354                 "SCSI Device",
1355                 "ATE Port",
1356                 "ATE Device",
1357                 "Floppy Controller",
1358                 "Floppy Device",
1359                 "Secondary Bus Port",
1360                 "Peer Transport Agent",
1361                 "Peer Transport",
1362                 "Unknown"
1363         };
1364         
1365         switch(class&0xFFF)
1366         {
1367                 case I2O_CLASS_EXECUTIVE:
1368                         idx = 0; break;
1369                 case I2O_CLASS_DDM:
1370                         idx = 1; break;
1371                 case I2O_CLASS_RANDOM_BLOCK_STORAGE:
1372                         idx = 2; break;
1373                 case I2O_CLASS_SEQUENTIAL_STORAGE:
1374                         idx = 3; break;
1375                 case I2O_CLASS_LAN:
1376                         idx = 4; break;
1377                 case I2O_CLASS_WAN:
1378                         idx = 5; break;
1379                 case I2O_CLASS_FIBRE_CHANNEL_PORT:
1380                         idx = 6; break;
1381                 case I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL:
1382                         idx = 7; break;
1383                 case I2O_CLASS_SCSI_PERIPHERAL:
1384                         idx = 8; break;
1385                 case I2O_CLASS_ATE_PORT:
1386                         idx = 9; break;
1387                 case I2O_CLASS_ATE_PERIPHERAL:
1388                         idx = 10; break;
1389                 case I2O_CLASS_FLOPPY_CONTROLLER:
1390                         idx = 11; break;
1391                 case I2O_CLASS_FLOPPY_DEVICE:
1392                         idx = 12; break;
1393                 case I2O_CLASS_BUS_ADAPTER_PORT:
1394                         idx = 13; break;
1395                 case I2O_CLASS_PEER_TRANSPORT_AGENT:
1396                         idx = 14; break;
1397                 case I2O_CLASS_PEER_TRANSPORT:
1398                         idx = 15; break;
1399         }
1400
1401         return i2o_class_name[idx];
1402 }
1403
1404
1405 /**
1406  *      i2o_wait_message        -       obtain an i2o message from the IOP
1407  *      @c: controller
1408  *      @why: explanation 
1409  *
1410  *      This function waits up to 5 seconds for a message slot to be
1411  *      available. If no message is available it prints an error message
1412  *      that is expected to be what the message will be used for (eg
1413  *      "get_status"). 0xFFFFFFFF is returned on a failure.
1414  *
1415  *      On a success the message is returned. This is the physical page
1416  *      frame offset address from the read port. (See the i2o spec)
1417  */
1418  
1419 u32 i2o_wait_message(struct i2o_controller *c, char *why)
1420 {
1421         long time=jiffies;
1422         u32 m;
1423         while((m=I2O_POST_READ32(c))==0xFFFFFFFF)
1424         {
1425                 if((jiffies-time)>=5*HZ)
1426                 {
1427                         dprintk(KERN_ERR "%s: Timeout waiting for message frame to send %s.\n", 
1428                                 c->name, why);
1429                         return 0xFFFFFFFF;
1430                 }
1431                 schedule();
1432                 barrier();
1433         }
1434         return m;
1435 }
1436         
1437 /**
1438  *      i2o_report_controller_unit - print information about a tid
1439  *      @c: controller
1440  *      @d: device
1441  *      
1442  *      Dump an information block associated with a given unit (TID). The
1443  *      tables are read and a block of text is output to printk that is
1444  *      formatted intended for the user.
1445  */
1446  
1447 void i2o_report_controller_unit(struct i2o_controller *c, struct i2o_device *d)
1448 {
1449         char buf[64];
1450         char str[22];
1451         int ret;
1452         int unit = d->lct_data.tid;
1453
1454         if(verbose==0)
1455                 return;
1456                 
1457         printk(KERN_INFO "Target ID %d.\n", unit);
1458         if((ret=i2o_query_scalar(c, unit, 0xF100, 3, buf, 16))>=0)
1459         {
1460                 buf[16]=0;
1461                 printk(KERN_INFO "     Vendor: %s\n", buf);
1462         }
1463         if((ret=i2o_query_scalar(c, unit, 0xF100, 4, buf, 16))>=0)
1464         {
1465                 buf[16]=0;
1466                 printk(KERN_INFO "     Device: %s\n", buf);
1467         }
1468         if(i2o_query_scalar(c, unit, 0xF100, 5, buf, 16)>=0)
1469         {
1470                 buf[16]=0;
1471                 printk(KERN_INFO "     Description: %s\n", buf);
1472         }
1473         if((ret=i2o_query_scalar(c, unit, 0xF100, 6, buf, 8))>=0)
1474         {
1475                 buf[8]=0;
1476                 printk(KERN_INFO "        Rev: %s\n", buf);
1477         }
1478
1479         printk(KERN_INFO "    Class: ");
1480         sprintf(str, "%-21s", i2o_get_class_name(d->lct_data.class_id));
1481         printk("%s\n", str);
1482                 
1483         printk(KERN_INFO "  Subclass: 0x%04X\n", d->lct_data.sub_class);
1484         printk(KERN_INFO "     Flags: ");
1485                 
1486         if(d->lct_data.device_flags&(1<<0))
1487                 printk("C");            // ConfigDialog requested
1488         if(d->lct_data.device_flags&(1<<1))
1489                 printk("U");            // Multi-user capable
1490         if(!(d->lct_data.device_flags&(1<<4)))
1491                 printk("P");            // Peer service enabled!
1492         if(!(d->lct_data.device_flags&(1<<5)))
1493                 printk("M");            // Mgmt service enabled!
1494         printk("\n");
1495                         
1496 }
1497
1498
1499 /*
1500  *      Parse the hardware resource table. Right now we print it out
1501  *      and don't do a lot with it. We should collate these and then
1502  *      interact with the Linux resource allocation block.
1503  *
1504  *      Lets prove we can read it first eh ?
1505  *
1506  *      This is full of endianisms!
1507  */
1508  
1509 static int i2o_parse_hrt(struct i2o_controller *c)
1510 {
1511 #ifdef DRIVERDEBUG
1512         u32 *rows=(u32*)c->hrt;
1513         u8 *p=(u8 *)c->hrt;
1514         u8 *d;
1515         int count;
1516         int length;
1517         int i;
1518         int state;
1519         
1520         if(p[3]!=0)
1521         {
1522                 printk(KERN_ERR "%s: HRT table for controller is too new a version.\n",
1523                         c->name);
1524                 return -1;
1525         }
1526                 
1527         count=p[0]|(p[1]<<8);
1528         length = p[2];
1529         
1530         printk(KERN_INFO "%s: HRT has %d entries of %d bytes each.\n",
1531                 c->name, count, length<<2);
1532
1533         rows+=2;
1534         
1535         for(i=0;i<count;i++)
1536         {
1537                 printk(KERN_INFO "Adapter %08X: ", rows[0]);
1538                 p=(u8 *)(rows+1);
1539                 d=(u8 *)(rows+2);
1540                 state=p[1]<<8|p[0];
1541                 
1542                 printk("TID %04X:[", state&0xFFF);
1543                 state>>=12;
1544                 if(state&(1<<0))
1545                         printk("H");            /* Hidden */
1546                 if(state&(1<<2))
1547                 {
1548                         printk("P");            /* Present */
1549                         if(state&(1<<1))
1550                                 printk("C");    /* Controlled */
1551                 }
1552                 if(state>9)
1553                         printk("*");            /* Hard */
1554                 
1555                 printk("]:");
1556                 
1557                 switch(p[3]&0xFFFF)
1558                 {
1559                         case 0:
1560                                 /* Adapter private bus - easy */
1561                                 printk("Local bus %d: I/O at 0x%04X Mem 0x%08X", 
1562                                         p[2], d[1]<<8|d[0], *(u32 *)(d+4));
1563                                 break;
1564                         case 1:
1565                                 /* ISA bus */
1566                                 printk("ISA %d: CSN %d I/O at 0x%04X Mem 0x%08X",
1567                                         p[2], d[2], d[1]<<8|d[0], *(u32 *)(d+4));
1568                                 break;
1569                                         
1570                         case 2: /* EISA bus */
1571                                 printk("EISA %d: Slot %d I/O at 0x%04X Mem 0x%08X",
1572                                         p[2], d[3], d[1]<<8|d[0], *(u32 *)(d+4));
1573                                 break;
1574
1575                         case 3: /* MCA bus */
1576                                 printk("MCA %d: Slot %d I/O at 0x%04X Mem 0x%08X",
1577                                         p[2], d[3], d[1]<<8|d[0], *(u32 *)(d+4));
1578                                 break;
1579
1580                         case 4: /* PCI bus */
1581                                 printk("PCI %d: Bus %d Device %d Function %d",
1582                                         p[2], d[2], d[1], d[0]);
1583                                 break;
1584
1585                         case 0x80: /* Other */
1586                         default:
1587                                 printk("Unsupported bus type.");
1588                                 break;
1589                 }
1590                 printk("\n");
1591                 rows+=length;
1592         }
1593 #endif
1594         return 0;
1595 }
1596         
1597 /*
1598  *      The logical configuration table tells us what we can talk to
1599  *      on the board. Most of the stuff isn't interesting to us. 
1600  */
1601
1602 static int i2o_parse_lct(struct i2o_controller *c)
1603 {
1604         int i;
1605         int max;
1606         int tid;
1607         struct i2o_device *d;
1608         i2o_lct *lct = c->lct;
1609
1610         if (lct == NULL) {
1611                 printk(KERN_ERR "%s: LCT is empty???\n", c->name);
1612                 return -1;
1613         }
1614
1615         max = lct->table_size;
1616         max -= 3;
1617         max /= 9;
1618         
1619         printk(KERN_INFO "%s: LCT has %d entries.\n", c->name, max);
1620         
1621         if(lct->iop_flags&(1<<0))
1622                 printk(KERN_WARNING "%s: Configuration dialog desired.\n", c->name);
1623                 
1624         for(i=0;i<max;i++)
1625         {
1626                 d = (struct i2o_device *)kmalloc(sizeof(struct i2o_device), GFP_KERNEL);
1627                 if(d==NULL)
1628                 {
1629                         printk(KERN_CRIT "i2o_core: Out of memory for I2O device data.\n");
1630                         return -ENOMEM;
1631                 }
1632                 
1633                 d->controller = c;
1634                 d->next = NULL;
1635
1636                 memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
1637
1638                 d->flags = 0;
1639                 tid = d->lct_data.tid;
1640                 
1641                 i2o_report_controller_unit(c, d);
1642                 
1643                 i2o_install_device(c, d);
1644         }
1645         return 0;
1646 }
1647
1648
1649 /**
1650  *      i2o_quiesce_controller - quiesce controller
1651  *      @c: controller 
1652  *
1653  *      Quiesce an IOP. Causes IOP to make external operation quiescent
1654  *      (i2o 'READY' state). Internal operation of the IOP continues normally.
1655  */
1656  
1657 int i2o_quiesce_controller(struct i2o_controller *c)
1658 {
1659         u32 msg[4];
1660         int ret;
1661
1662         i2o_status_get(c);
1663
1664         /* SysQuiesce discarded if IOP not in READY or OPERATIONAL state */
1665
1666         if ((c->status_block->iop_state != ADAPTER_STATE_READY) &&
1667                 (c->status_block->iop_state != ADAPTER_STATE_OPERATIONAL))
1668         {
1669                 return 0;
1670         }
1671
1672         msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
1673         msg[1] = I2O_CMD_SYS_QUIESCE<<24|HOST_TID<<12|ADAPTER_TID;
1674         msg[3] = 0;
1675
1676         /* Long timeout needed for quiesce if lots of devices */
1677
1678         if ((ret = i2o_post_wait(c, msg, sizeof(msg), 240)))
1679                 printk(KERN_INFO "%s: Unable to quiesce (status=%#x).\n",
1680                         c->name, -ret);
1681         else
1682                 dprintk(KERN_INFO "%s: Quiesced.\n", c->name);
1683
1684         i2o_status_get(c); // Entered READY state
1685         return ret;
1686 }
1687
1688 /**
1689  *      i2o_enable_controller - move controller from ready to operational
1690  *      @c: controller
1691  *
1692  *      Enable IOP. This allows the IOP to resume external operations and
1693  *      reverses the effect of a quiesce. In the event of an error a negative
1694  *      errno code is returned.
1695  */
1696  
1697 int i2o_enable_controller(struct i2o_controller *c)
1698 {
1699         u32 msg[4];
1700         int ret;
1701
1702         i2o_status_get(c);
1703         
1704         /* Enable only allowed on READY state */        
1705         if(c->status_block->iop_state != ADAPTER_STATE_READY)
1706                 return -EINVAL;
1707
1708         msg[0]=FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
1709         msg[1]=I2O_CMD_SYS_ENABLE<<24|HOST_TID<<12|ADAPTER_TID;
1710
1711         /* How long of a timeout do we need? */
1712
1713         if ((ret = i2o_post_wait(c, msg, sizeof(msg), 240)))
1714                 printk(KERN_ERR "%s: Could not enable (status=%#x).\n",
1715                         c->name, -ret);
1716         else
1717                 dprintk(KERN_INFO "%s: Enabled.\n", c->name);
1718
1719         i2o_status_get(c); // entered OPERATIONAL state
1720
1721         return ret;
1722 }
1723
1724 /**
1725  *      i2o_clear_controller    -       clear a controller
1726  *      @c: controller
1727  *
1728  *      Clear an IOP to HOLD state, ie. terminate external operations, clear all
1729  *      input queues and prepare for a system restart. IOP's internal operation
1730  *      continues normally and the outbound queue is alive.
1731  *      The IOP is not expected to rebuild its LCT.
1732  */
1733  
1734 int i2o_clear_controller(struct i2o_controller *c)
1735 {
1736         struct i2o_controller *iop;
1737         u32 msg[4];
1738         int ret;
1739
1740         /* Quiesce all IOPs first */
1741
1742         for (iop = i2o_controller_chain; iop; iop = iop->next)
1743                 i2o_quiesce_controller(iop);
1744
1745         msg[0]=FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
1746         msg[1]=I2O_CMD_ADAPTER_CLEAR<<24|HOST_TID<<12|ADAPTER_TID;
1747         msg[3]=0;
1748
1749         if ((ret=i2o_post_wait(c, msg, sizeof(msg), 30)))
1750                 printk(KERN_INFO "%s: Unable to clear (status=%#x).\n",
1751                         c->name, -ret);
1752         else
1753                 dprintk(KERN_INFO "%s: Cleared.\n",c->name);
1754
1755         i2o_status_get(c);
1756
1757         /* Enable other IOPs */
1758
1759         for (iop = i2o_controller_chain; iop; iop = iop->next)
1760                 if (iop != c)
1761                         i2o_enable_controller(iop);
1762
1763         return ret;
1764 }
1765
1766
1767 /**
1768  *      i2o_reset_controller    -       reset an IOP
1769  *      @c: controller to reset
1770  *
1771  *      Reset the IOP into INIT state and wait until IOP gets into RESET state.
1772  *      Terminate all external operations, clear IOP's inbound and outbound
1773  *      queues, terminate all DDMs, and reload the IOP's operating environment
1774  *      and all local DDMs. The IOP rebuilds its LCT.
1775  */
1776  
1777 static int i2o_reset_controller(struct i2o_controller *c)
1778 {
1779         struct i2o_controller *iop;
1780         u32 m;
1781         u8 *status;
1782         dma_addr_t status_phys;
1783         u32 *msg;
1784         long time;
1785
1786         /* Quiesce all IOPs first */
1787
1788         for (iop = i2o_controller_chain; iop; iop = iop->next)
1789         {
1790                 if(!iop->dpt)
1791                         i2o_quiesce_controller(iop);
1792         }
1793
1794         m=i2o_wait_message(c, "AdapterReset");
1795         if(m==0xFFFFFFFF)       
1796                 return -ETIMEDOUT;
1797         msg=(u32 *)(c->mem_offset+m);
1798         
1799         status = pci_alloc_consistent(c->pdev, 4, &status_phys);
1800         if(status == NULL) {
1801                 printk(KERN_ERR "IOP reset failed - no free memory.\n");
1802                 return -ENOMEM;
1803         }
1804         memset(status, 0, 4);
1805         
1806         msg[0]=EIGHT_WORD_MSG_SIZE|SGL_OFFSET_0;
1807         msg[1]=I2O_CMD_ADAPTER_RESET<<24|HOST_TID<<12|ADAPTER_TID;
1808         msg[2]=core_context;
1809         msg[3]=0;
1810         msg[4]=0;
1811         msg[5]=0;
1812         msg[6]=status_phys;
1813         msg[7]=0;       /* 64bit host FIXME */
1814
1815         i2o_post_message(c,m);
1816
1817         /* Wait for a reply */
1818         time=jiffies;
1819         while(*status==0)
1820         {
1821                 if((jiffies-time)>=20*HZ)
1822                 {
1823                         printk(KERN_ERR "IOP reset timeout.\n");
1824                         /* The controller still may respond and overwrite
1825                          * status_phys, LEAK it to prevent memory corruption.
1826                          */
1827                         return -ETIMEDOUT;
1828                 }
1829                 schedule();
1830                 barrier();
1831         }
1832
1833         if (*status==I2O_CMD_IN_PROGRESS)
1834         { 
1835                 /* 
1836                  * Once the reset is sent, the IOP goes into the INIT state 
1837                  * which is indeterminate.  We need to wait until the IOP 
1838                  * has rebooted before we can let the system talk to 
1839                  * it. We read the inbound Free_List until a message is 
1840                  * available.  If we can't read one in the given ammount of 
1841                  * time, we assume the IOP could not reboot properly.  
1842                  */ 
1843
1844                 dprintk(KERN_INFO "%s: Reset in progress, waiting for reboot...\n",
1845                         c->name); 
1846
1847                 time = jiffies; 
1848                 m = I2O_POST_READ32(c); 
1849                 while(m == 0XFFFFFFFF) 
1850                 { 
1851                         if((jiffies-time) >= 30*HZ)
1852                         {
1853                                 printk(KERN_ERR "%s: Timeout waiting for IOP reset.\n", 
1854                                                 c->name); 
1855                                 /* The controller still may respond and
1856                                  * overwrite status_phys, LEAK it to prevent
1857                                  * memory corruption.
1858                                  */
1859                                 return -ETIMEDOUT; 
1860                         } 
1861                         schedule(); 
1862                         barrier(); 
1863                         m = I2O_POST_READ32(c); 
1864                 }
1865                 i2o_flush_reply(c,m);
1866         }
1867
1868         /* If IopReset was rejected or didn't perform reset, try IopClear */
1869
1870         i2o_status_get(c);
1871         if (status[0] == I2O_CMD_REJECTED || 
1872                 c->status_block->iop_state != ADAPTER_STATE_RESET)
1873         {
1874                 printk(KERN_WARNING "%s: Reset rejected, trying to clear\n",c->name);
1875                 i2o_clear_controller(c);
1876         }
1877         else
1878                 dprintk(KERN_INFO "%s: Reset completed.\n", c->name);
1879
1880         /* Enable other IOPs */
1881
1882         for (iop = i2o_controller_chain; iop; iop = iop->next)
1883                 if (iop != c)
1884                         i2o_enable_controller(iop);
1885
1886         pci_free_consistent(c->pdev, 4, status, status_phys);
1887         return 0;
1888 }
1889
1890
1891 /**
1892  *      i2o_status_get  -       get the status block for the IOP
1893  *      @c: controller
1894  *
1895  *      Issue a status query on the controller. This updates the
1896  *      attached status_block. If the controller fails to reply or an
1897  *      error occurs then a negative errno code is returned. On success
1898  *      zero is returned and the status_blok is updated.
1899  */
1900  
1901 int i2o_status_get(struct i2o_controller *c)
1902 {
1903         long time;
1904         u32 m;
1905         u32 *msg;
1906         u8 *status_block;
1907
1908         if (c->status_block == NULL) 
1909         {
1910                 c->status_block = (i2o_status_block *)
1911                         pci_alloc_consistent(c->pdev, sizeof(i2o_status_block), &c->status_block_phys);
1912                 if (c->status_block == NULL)
1913                 {
1914                         printk(KERN_CRIT "%s: Get Status Block failed; Out of memory.\n",
1915                                 c->name);
1916                         return -ENOMEM;
1917                 }
1918         }
1919
1920         status_block = (u8*)c->status_block;
1921         memset(c->status_block,0,sizeof(i2o_status_block));
1922         
1923         m=i2o_wait_message(c, "StatusGet");
1924         if(m==0xFFFFFFFF)
1925                 return -ETIMEDOUT;      
1926         msg=(u32 *)(c->mem_offset+m);
1927
1928         msg[0]=NINE_WORD_MSG_SIZE|SGL_OFFSET_0;
1929         msg[1]=I2O_CMD_STATUS_GET<<24|HOST_TID<<12|ADAPTER_TID;
1930         msg[2]=core_context;
1931         msg[3]=0;
1932         msg[4]=0;
1933         msg[5]=0;
1934         msg[6]=c->status_block_phys;
1935         msg[7]=0;   /* 64bit host FIXME */
1936         msg[8]=sizeof(i2o_status_block); /* always 88 bytes */
1937
1938         i2o_post_message(c,m);
1939
1940         /* Wait for a reply */
1941
1942         time=jiffies;
1943         while(status_block[87]!=0xFF)
1944         {
1945                 if((jiffies-time)>=5*HZ)
1946                 {
1947                         printk(KERN_ERR "%s: Get status timeout.\n",c->name);
1948                         return -ETIMEDOUT;
1949                 }
1950                 yield();
1951                 barrier();
1952         }
1953
1954 #ifdef DRIVERDEBUG
1955         printk(KERN_INFO "%s: State = ", c->name);
1956         switch (c->status_block->iop_state) {
1957                 case 0x01:  
1958                         printk("INIT\n");
1959                         break;
1960                 case 0x02:
1961                         printk("RESET\n");
1962                         break;
1963                 case 0x04:
1964                         printk("HOLD\n");
1965                         break;
1966                 case 0x05:
1967                         printk("READY\n");
1968                         break;
1969                 case 0x08:
1970                         printk("OPERATIONAL\n");
1971                         break;
1972                 case 0x10:
1973                         printk("FAILED\n");
1974                         break;
1975                 case 0x11:
1976                         printk("FAULTED\n");
1977                         break;
1978                 default: 
1979                         printk("%x (unknown !!)\n",c->status_block->iop_state);
1980 }     
1981 #endif   
1982
1983         return 0;
1984 }
1985
1986 /*
1987  * Get the Hardware Resource Table for the device.
1988  * The HRT contains information about possible hidden devices
1989  * but is mostly useless to us 
1990  */
1991 int i2o_hrt_get(struct i2o_controller *c)
1992 {
1993         u32 msg[6];
1994         int ret, size = sizeof(i2o_hrt);
1995         int loops = 3;  /* we only try 3 times to get the HRT, this should be
1996                            more then enough. Worst case should be 2 times.*/
1997
1998         /* First read just the header to figure out the real size */
1999
2000         do  {
2001                 /* first we allocate the memory for the HRT */
2002                 if (c->hrt == NULL) {
2003                         c->hrt=pci_alloc_consistent(c->pdev, size, &c->hrt_phys);
2004                         if (c->hrt == NULL) {
2005                                 printk(KERN_CRIT "%s: Hrt Get failed; Out of memory.\n", c->name);
2006                                 return -ENOMEM;
2007                         }
2008                         c->hrt_len = size;
2009                 }
2010
2011                 msg[0]= SIX_WORD_MSG_SIZE| SGL_OFFSET_4;
2012                 msg[1]= I2O_CMD_HRT_GET<<24 | HOST_TID<<12 | ADAPTER_TID;
2013                 msg[3]= 0;
2014                 msg[4]= (0xD0000000 | c->hrt_len);      /* Simple transaction */
2015                 msg[5]= c->hrt_phys;            /* Dump it here */
2016
2017                 ret = i2o_post_wait_mem(c, msg, sizeof(msg), 20, c->hrt, NULL, c->hrt_phys, 0, c->hrt_len, 0);
2018                 
2019                 if(ret == -ETIMEDOUT)
2020                 {
2021                         /* The HRT block we used is in limbo somewhere. When the iop wakes up
2022                            we will recover it */
2023                         c->hrt = NULL;
2024                         c->hrt_len = 0;
2025                         return ret;
2026                 }
2027                 
2028                 if(ret<0)
2029                 {
2030                         printk(KERN_ERR "%s: Unable to get HRT (status=%#x)\n",
2031                                 c->name, -ret); 
2032                         return ret;
2033                 }
2034
2035                 if (c->hrt->num_entries * c->hrt->entry_len << 2 > c->hrt_len) {
2036                         size = c->hrt->num_entries * c->hrt->entry_len << 2;
2037                         pci_free_consistent(c->pdev, c->hrt_len, c->hrt, c->hrt_phys);
2038                         c->hrt_len = 0;
2039                         c->hrt = NULL;
2040                 }
2041                 loops --;
2042         } while (c->hrt == NULL && loops > 0);
2043
2044         if(c->hrt == NULL)
2045         {
2046                 printk(KERN_ERR "%s: Unable to get HRT after three tries, giving up\n", c->name);
2047                 return -1;
2048         }
2049
2050         i2o_parse_hrt(c); // just for debugging
2051
2052         return 0;
2053 }
2054
2055 /*
2056  * Send the I2O System Table to the specified IOP
2057  *
2058  * The system table contains information about all the IOPs in the
2059  * system.  It is build and then sent to each IOP so that IOPs can
2060  * establish connections between each other.
2061  *
2062  */
2063 static int i2o_systab_send(struct i2o_controller *iop)
2064 {
2065         u32 msg[12];
2066         dma_addr_t sys_tbl_phys;
2067         int ret;
2068         struct resource *root;
2069         u32 *privbuf = kmalloc(16, GFP_KERNEL);
2070         if(privbuf == NULL)
2071                 return -ENOMEM;
2072         
2073                 
2074         if(iop->status_block->current_mem_size < iop->status_block->desired_mem_size)
2075         {
2076                 struct resource *res = &iop->mem_resource;
2077                 res->name = iop->pdev->bus->name;
2078                 res->flags = IORESOURCE_MEM;
2079                 res->start = 0;
2080                 res->end = 0;
2081                 printk("%s: requires private memory resources.\n", iop->name);
2082                 root = pci_find_parent_resource(iop->pdev, res);
2083                 if(root==NULL)
2084                         printk("Can't find parent resource!\n");
2085                 if(root && allocate_resource(root, res, 
2086                                 iop->status_block->desired_mem_size,
2087                                 iop->status_block->desired_mem_size,
2088                                 iop->status_block->desired_mem_size,
2089                                 1<<20,  /* Unspecified, so use 1Mb and play safe */
2090                                 NULL,
2091                                 NULL)>=0)
2092                 {
2093                         iop->mem_alloc = 1;
2094                         iop->status_block->current_mem_size = 1 + res->end - res->start;
2095                         iop->status_block->current_mem_base = res->start;
2096                         printk(KERN_INFO "%s: allocated %ld bytes of PCI memory at 0x%08lX.\n", 
2097                                 iop->name, 1+res->end-res->start, res->start);
2098                 }
2099         }
2100         if(iop->status_block->current_io_size < iop->status_block->desired_io_size)
2101         {
2102                 struct resource *res = &iop->io_resource;
2103                 res->name = iop->pdev->bus->name;
2104                 res->flags = IORESOURCE_IO;
2105                 res->start = 0;
2106                 res->end = 0;
2107                 printk("%s: requires private memory resources.\n", iop->name);
2108                 root = pci_find_parent_resource(iop->pdev, res);
2109                 if(root==NULL)
2110                         printk("Can't find parent resource!\n");
2111                 if(root &&  allocate_resource(root, res, 
2112                                 iop->status_block->desired_io_size,
2113                                 iop->status_block->desired_io_size,
2114                                 iop->status_block->desired_io_size,
2115                                 1<<20,  /* Unspecified, so use 1Mb and play safe */
2116                                 NULL,
2117                                 NULL)>=0)
2118                 {
2119                         iop->io_alloc = 1;
2120                         iop->status_block->current_io_size = 1 + res->end - res->start;
2121                         iop->status_block->current_mem_base = res->start;
2122                         printk(KERN_INFO "%s: allocated %ld bytes of PCI I/O at 0x%08lX.\n", 
2123                                 iop->name, 1+res->end-res->start, res->start);
2124                 }
2125         }
2126         else
2127         {       
2128                 privbuf[0] = iop->status_block->current_mem_base;
2129                 privbuf[1] = iop->status_block->current_mem_size;
2130                 privbuf[2] = iop->status_block->current_io_base;
2131                 privbuf[3] = iop->status_block->current_io_size;
2132         }
2133
2134         msg[0] = I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6;
2135         msg[1] = I2O_CMD_SYS_TAB_SET<<24 | HOST_TID<<12 | ADAPTER_TID;
2136         msg[3] = 0;
2137         msg[4] = (0<<16) | ((iop->unit+2) );      /* Host 0 IOP ID (unit + 2) */
2138         msg[5] = 0;                               /* Segment 0 */
2139
2140         /* 
2141          * Provide three SGL-elements:
2142          * System table (SysTab), Private memory space declaration and 
2143          * Private i/o space declaration  
2144          * 
2145          * Nasty one here. We can't use pci_alloc_consistent to send the
2146          * same table to everyone. We have to go remap it for them all
2147          */
2148          
2149         sys_tbl_phys = pci_map_single(iop->pdev, sys_tbl, sys_tbl_len, PCI_DMA_TODEVICE);
2150         msg[6] = 0x54000000 | sys_tbl_phys;
2151
2152         msg[7] = sys_tbl_phys;
2153         msg[8] = 0x54000000 | privbuf[1];
2154         msg[9] = privbuf[0];
2155         msg[10] = 0xD4000000 | privbuf[3];
2156         msg[11] = privbuf[2];
2157
2158         ret=i2o_post_wait(iop, msg, sizeof(msg), 120);
2159
2160         pci_unmap_single(iop->pdev, sys_tbl_phys, sys_tbl_len, PCI_DMA_TODEVICE);
2161         
2162         if(ret==-ETIMEDOUT)
2163         {
2164                 printk(KERN_ERR "%s: SysTab setup timed out.\n", iop->name);
2165         }
2166         else if(ret<0)
2167         {
2168                 printk(KERN_ERR "%s: Unable to set SysTab (status=%#x).\n", 
2169                         iop->name, -ret);
2170         }
2171         else
2172         {
2173                 dprintk(KERN_INFO "%s: SysTab set.\n", iop->name);
2174         }
2175         i2o_status_get(iop); // Entered READY state
2176
2177         kfree(privbuf);
2178         return ret;     
2179
2180  }
2181
2182 /*
2183  * Initialize I2O subsystem.
2184  */
2185 void __init i2o_sys_init(void)
2186 {
2187         struct i2o_controller *iop, *niop = NULL;
2188
2189         printk(KERN_INFO "Activating I2O controllers...\n");
2190         printk(KERN_INFO "This may take a few minutes if there are many devices\n");
2191         
2192         /* In INIT state, Activate IOPs */
2193         for (iop = i2o_controller_chain; iop; iop = niop) {
2194                 dprintk(KERN_INFO "Calling i2o_activate_controller for %s...\n", 
2195                         iop->name);
2196                 niop = iop->next;
2197                 if (i2o_activate_controller(iop) < 0)
2198                         i2o_delete_controller(iop);
2199         }
2200
2201         /* Active IOPs in HOLD state */
2202
2203 rebuild_sys_tab:
2204         if (i2o_controller_chain == NULL)
2205                 return;
2206
2207         /*
2208          * If build_sys_table fails, we kill everything and bail
2209          * as we can't init the IOPs w/o a system table
2210          */     
2211         dprintk(KERN_INFO "i2o_core: Calling i2o_build_sys_table...\n");
2212         if (i2o_build_sys_table() < 0) {
2213                 i2o_sys_shutdown();
2214                 return;
2215         }
2216
2217         /* If IOP don't get online, we need to rebuild the System table */
2218         for (iop = i2o_controller_chain; iop; iop = niop) {
2219                 niop = iop->next;
2220                 dprintk(KERN_INFO "Calling i2o_online_controller for %s...\n", iop->name);
2221                 if (i2o_online_controller(iop) < 0) {
2222                         i2o_delete_controller(iop);     
2223                         goto rebuild_sys_tab;
2224                 }
2225         }
2226         
2227         /* Active IOPs now in OPERATIONAL state */
2228
2229         /*
2230          * Register for status updates from all IOPs
2231          */
2232         for(iop = i2o_controller_chain; iop; iop=iop->next) {
2233
2234                 /* Create a kernel thread to deal with dynamic LCT updates */
2235                 iop->lct_pid = kernel_thread(i2o_dyn_lct, iop, CLONE_SIGHAND);
2236         
2237                 /* Update change ind on DLCT */
2238                 iop->dlct->change_ind = iop->lct->change_ind;
2239
2240                 /* Start dynamic LCT updates */
2241                 i2o_lct_notify(iop);
2242
2243                 /* Register for all events from IRTOS */
2244                 i2o_event_register(iop, core_context, 0, 0, 0xFFFFFFFF);
2245         }
2246 }
2247
2248 /**
2249  *      i2o_sys_shutdown - shutdown I2O system
2250  *
2251  *      Bring down each i2o controller and then return. Each controller
2252  *      is taken through an orderly shutdown
2253  */
2254  
2255 static void i2o_sys_shutdown(void)
2256 {
2257         struct i2o_controller *iop, *niop;
2258
2259         /* Delete all IOPs from the controller chain */
2260         /* that will reset all IOPs too */
2261
2262         for (iop = i2o_controller_chain; iop; iop = niop) {
2263                 niop = iop->next;
2264                 i2o_delete_controller(iop);
2265         }
2266 }
2267
2268 /**
2269  *      i2o_activate_controller -       bring controller up to HOLD
2270  *      @iop: controller
2271  *
2272  *      This function brings an I2O controller into HOLD state. The adapter
2273  *      is reset if necessary and then the queues and resource table
2274  *      are read. -1 is returned on a failure, 0 on success.
2275  *      
2276  */
2277  
2278 int i2o_activate_controller(struct i2o_controller *iop)
2279 {
2280         /* In INIT state, Wait Inbound Q to initialize (in i2o_status_get) */
2281         /* In READY state, Get status */
2282
2283         if (i2o_status_get(iop) < 0) {
2284                 printk(KERN_INFO "Unable to obtain status of %s, "
2285                         "attempting a reset.\n", iop->name);
2286                 if (i2o_reset_controller(iop) < 0)
2287                         return -1;
2288         }
2289
2290         if(iop->status_block->iop_state == ADAPTER_STATE_FAULTED) {
2291                 printk(KERN_CRIT "%s: hardware fault\n", iop->name);
2292                 return -1;
2293         }
2294
2295         if (iop->status_block->i2o_version > I2OVER15) {
2296                 printk(KERN_ERR "%s: Not running vrs. 1.5. of the I2O Specification.\n",
2297                         iop->name);
2298                 return -1;
2299         }
2300
2301         if (iop->status_block->iop_state == ADAPTER_STATE_READY ||
2302             iop->status_block->iop_state == ADAPTER_STATE_OPERATIONAL ||
2303             iop->status_block->iop_state == ADAPTER_STATE_HOLD ||
2304             iop->status_block->iop_state == ADAPTER_STATE_FAILED)
2305         {
2306                 dprintk(KERN_INFO "%s: Already running, trying to reset...\n",
2307                         iop->name);
2308                 if (i2o_reset_controller(iop) < 0)
2309                         return -1;
2310         }
2311
2312         if (i2o_init_outbound_q(iop) < 0)
2313                 return -1;
2314
2315         if (i2o_post_outbound_messages(iop)) 
2316                 return -1;
2317
2318         /* In HOLD state */
2319         
2320         if (i2o_hrt_get(iop) < 0)
2321                 return -1;
2322
2323         return 0;
2324 }
2325
2326
2327 /**
2328  *      i2o_init_outbound_queue - setup the outbound queue
2329  *      @c: controller
2330  *
2331  *      Clear and (re)initialize IOP's outbound queue. Returns 0 on
2332  *      success or a negative errno code on a failure.
2333  */
2334  
2335 int i2o_init_outbound_q(struct i2o_controller *c)
2336 {
2337         u8 *status;
2338         dma_addr_t status_phys;
2339         u32 m;
2340         u32 *msg;
2341         u32 time;
2342
2343         dprintk(KERN_INFO "%s: Initializing Outbound Queue...\n", c->name);
2344         m=i2o_wait_message(c, "OutboundInit");
2345         if(m==0xFFFFFFFF)
2346                 return -ETIMEDOUT;
2347         msg=(u32 *)(c->mem_offset+m);
2348
2349         status = pci_alloc_consistent(c->pdev, 4, &status_phys);
2350         if (status==NULL) {
2351                 printk(KERN_ERR "%s: Outbound Queue initialization failed - no free memory.\n",
2352                         c->name);
2353                 return -ENOMEM;
2354         }
2355         memset(status, 0, 4);
2356
2357         msg[0]= EIGHT_WORD_MSG_SIZE| TRL_OFFSET_6;
2358         msg[1]= I2O_CMD_OUTBOUND_INIT<<24 | HOST_TID<<12 | ADAPTER_TID;
2359         msg[2]= core_context;
2360         msg[3]= 0x0106;                         /* Transaction context */
2361         msg[4]= 4096;                           /* Host page frame size */
2362         /* Frame size is in words. 256 bytes a frame for now */
2363         msg[5]= MSG_FRAME_SIZE<<16|0x80;        /* Outbound msg frame size in words and Initcode */
2364         msg[6]= 0xD0000004;                     /* Simple SG LE, EOB */
2365         msg[7]= status_phys;
2366
2367         i2o_post_message(c,m);
2368         
2369         barrier();
2370         time=jiffies;
2371         while(status[0] < I2O_CMD_REJECTED)
2372         {
2373                 if((jiffies-time)>=30*HZ)
2374                 {
2375                         if(status[0]==0x00)
2376                                 printk(KERN_ERR "%s: Ignored queue initialize request.\n",
2377                                         c->name);
2378                         else  
2379                                 printk(KERN_ERR "%s: Outbound queue initialize timeout.\n",
2380                                         c->name);
2381                         pci_free_consistent(c->pdev, 4, status, status_phys);
2382                         return -ETIMEDOUT;
2383                 }  
2384                 yield();
2385                 barrier();
2386         }  
2387
2388         if(status[0] != I2O_CMD_COMPLETED)
2389         {
2390                 printk(KERN_ERR "%s: IOP outbound initialise failed.\n", c->name);
2391                 pci_free_consistent(c->pdev, 4, status, status_phys);
2392                 return -ETIMEDOUT;
2393         }
2394         pci_free_consistent(c->pdev, 4, status, status_phys);
2395         return 0;
2396 }
2397
2398 /**
2399  *      i2o_post_outbound_messages      -       fill message queue
2400  *      @c: controller
2401  *
2402  *      Allocate a message frame and load the messages into the IOP. The
2403  *      function returns zero on success or a negative errno code on
2404  *      failure.
2405  */
2406
2407 int i2o_post_outbound_messages(struct i2o_controller *c)
2408 {
2409         int i;
2410         u32 m;
2411         /* Alloc space for IOP's outbound queue message frames */
2412
2413         c->page_frame = kmalloc(MSG_POOL_SIZE, GFP_KERNEL);
2414         if(c->page_frame==NULL) {
2415                 printk(KERN_ERR "%s: Outbound Q initialize failed; out of memory.\n",
2416                         c->name);
2417                 return -ENOMEM;
2418         }
2419
2420         c->page_frame_map = pci_map_single(c->pdev, c->page_frame, MSG_POOL_SIZE, PCI_DMA_FROMDEVICE);
2421
2422         if(c->page_frame_map == 0)
2423         {
2424                 kfree(c->page_frame);
2425                 printk(KERN_ERR "%s: Unable to map outbound queue.\n", c->name);
2426                 return -ENOMEM;
2427         }
2428
2429         m = c->page_frame_map;
2430
2431         /* Post frames */
2432
2433         for(i=0; i< NMBR_MSG_FRAMES; i++) {
2434                 I2O_REPLY_WRITE32(c,m);
2435                 mb();
2436                 m += (MSG_FRAME_SIZE << 2);
2437         }
2438
2439         return 0;
2440 }
2441
2442 /*
2443  * Get the IOP's Logical Configuration Table
2444  */
2445 int i2o_lct_get(struct i2o_controller *c)
2446 {
2447         u32 msg[8];
2448         int ret, size = c->status_block->expected_lct_size;
2449
2450         do {
2451                 if (c->lct == NULL) {
2452                         c->lct = pci_alloc_consistent(c->pdev, size, &c->lct_phys);
2453                         if(c->lct == NULL) {
2454                                 printk(KERN_CRIT "%s: Lct Get failed. Out of memory.\n",
2455                                         c->name);
2456                                 return -ENOMEM;
2457                         }
2458                 }
2459                 memset(c->lct, 0, size);
2460
2461                 msg[0] = EIGHT_WORD_MSG_SIZE|SGL_OFFSET_6;
2462                 msg[1] = I2O_CMD_LCT_NOTIFY<<24 | HOST_TID<<12 | ADAPTER_TID;
2463                 /* msg[2] filled in i2o_post_wait */
2464                 msg[3] = 0;
2465                 msg[4] = 0xFFFFFFFF;    /* All devices */
2466                 msg[5] = 0x00000000;    /* Report now */
2467                 msg[6] = 0xD0000000|size;
2468                 msg[7] = c->lct_phys;
2469
2470                 ret=i2o_post_wait_mem(c, msg, sizeof(msg), 120, c->lct, NULL, c->lct_phys, 0, size, 0);
2471                 
2472                 if(ret == -ETIMEDOUT)
2473                 {
2474                         c->lct = NULL;
2475                         return ret;
2476                 }
2477                 
2478                 if(ret<0)
2479                 {
2480                         printk(KERN_ERR "%s: LCT Get failed (status=%#x.\n", 
2481                                 c->name, -ret); 
2482                         return ret;
2483                 }
2484
2485                 if (c->lct->table_size << 2 > size) {
2486                         int new_size = c->lct->table_size << 2;
2487                         pci_free_consistent(c->pdev, size, c->lct, c->lct_phys);
2488                         size = new_size;
2489                         c->lct = NULL;
2490                 }
2491         } while (c->lct == NULL);
2492
2493         if ((ret=i2o_parse_lct(c)) < 0)
2494                 return ret;
2495
2496         return 0;
2497 }
2498
2499 /*
2500  * Like above, but used for async notification.  The main
2501  * difference is that we keep track of the CurrentChangeIndiicator
2502  * so that we only get updates when it actually changes.
2503  *
2504  */
2505 int i2o_lct_notify(struct i2o_controller *c)
2506 {
2507         u32 msg[8];
2508
2509         msg[0] = EIGHT_WORD_MSG_SIZE|SGL_OFFSET_6;
2510         msg[1] = I2O_CMD_LCT_NOTIFY<<24 | HOST_TID<<12 | ADAPTER_TID;
2511         msg[2] = core_context;
2512         msg[3] = 0xDEADBEEF;    
2513         msg[4] = 0xFFFFFFFF;    /* All devices */
2514         msg[5] = c->dlct->change_ind+1; /* Next change */
2515         msg[6] = 0xD0000000|8192;
2516         msg[7] = c->dlct_phys;
2517
2518         return i2o_post_this(c, msg, sizeof(msg));
2519 }
2520                 
2521 /*
2522  *      Bring a controller online into OPERATIONAL state. 
2523  */
2524  
2525 int i2o_online_controller(struct i2o_controller *iop)
2526 {
2527         u32 v;
2528         
2529         if (i2o_systab_send(iop) < 0)
2530                 return -1;
2531
2532         /* In READY state */
2533
2534         dprintk(KERN_INFO "%s: Attempting to enable...\n", iop->name);
2535         if (i2o_enable_controller(iop) < 0)
2536                 return -1;
2537
2538         /* In OPERATIONAL state  */
2539
2540         dprintk(KERN_INFO "%s: Attempting to get/parse lct...\n", iop->name);
2541         if (i2o_lct_get(iop) < 0)
2542                 return -1;
2543
2544         /* Check battery status */
2545          
2546         iop->battery = 0;
2547         if(i2o_query_scalar(iop, ADAPTER_TID, 0x0000, 4, &v, 4)>=0)
2548         {
2549                 if(v&16)
2550                         iop->battery = 1;
2551         }
2552
2553         return 0;
2554 }
2555
2556 /*
2557  * Build system table
2558  *
2559  * The system table contains information about all the IOPs in the
2560  * system (duh) and is used by the Executives on the IOPs to establish
2561  * peer2peer connections.  We're not supporting peer2peer at the moment,
2562  * but this will be needed down the road for things like lan2lan forwarding.
2563  */
2564 static int i2o_build_sys_table(void)
2565 {
2566         struct i2o_controller *iop = NULL;
2567         struct i2o_controller *niop = NULL;
2568         int count = 0;
2569
2570         sys_tbl_len = sizeof(struct i2o_sys_tbl) +      // Header + IOPs
2571                                 (i2o_num_controllers) *
2572                                         sizeof(struct i2o_sys_tbl_entry);
2573
2574         if(sys_tbl)
2575                 kfree(sys_tbl);
2576
2577         sys_tbl = kmalloc(sys_tbl_len, GFP_KERNEL);
2578         if(!sys_tbl) {
2579                 printk(KERN_CRIT "SysTab Set failed. Out of memory.\n");
2580                 return -ENOMEM;
2581         }
2582         memset((void*)sys_tbl, 0, sys_tbl_len);
2583
2584         sys_tbl->num_entries = i2o_num_controllers;
2585         sys_tbl->version = I2OVERSION; /* TODO: Version 2.0 */
2586         sys_tbl->change_ind = sys_tbl_ind++;
2587
2588         for(iop = i2o_controller_chain; iop; iop = niop)
2589         {
2590                 niop = iop->next;
2591
2592                 /* 
2593                  * Get updated IOP state so we have the latest information
2594                  *
2595                  * We should delete the controller at this point if it
2596                  * doesn't respond since  if it's not on the system table 
2597                  * it is techninically not part of the I2O subsyßtem...
2598                  */
2599                 if(i2o_status_get(iop)) {
2600                         printk(KERN_ERR "%s: Deleting b/c could not get status while"
2601                                 "attempting to build system table\n", iop->name);
2602                         i2o_delete_controller(iop);             
2603                         sys_tbl->num_entries--;
2604                         continue; // try the next one
2605                 }
2606
2607                 sys_tbl->iops[count].org_id = iop->status_block->org_id;
2608                 sys_tbl->iops[count].iop_id = iop->unit + 2;
2609                 sys_tbl->iops[count].seg_num = 0;
2610                 sys_tbl->iops[count].i2o_version = 
2611                                 iop->status_block->i2o_version;
2612                 sys_tbl->iops[count].iop_state = 
2613                                 iop->status_block->iop_state;
2614                 sys_tbl->iops[count].msg_type = 
2615                                 iop->status_block->msg_type;
2616                 sys_tbl->iops[count].frame_size = 
2617                                 iop->status_block->inbound_frame_size;
2618                 sys_tbl->iops[count].last_changed = sys_tbl_ind - 1; // ??
2619                 sys_tbl->iops[count].iop_capabilities = 
2620                                 iop->status_block->iop_capabilities;
2621                 sys_tbl->iops[count].inbound_low = iop->post_port;
2622                 sys_tbl->iops[count].inbound_high = 0;  // FIXME: 64-bit support
2623
2624                 count++;
2625         }
2626
2627 #ifdef DRIVERDEBUG
2628 {
2629         u32 *table;
2630         table = (u32*)sys_tbl;
2631         for(count = 0; count < (sys_tbl_len >>2); count++)
2632                 printk(KERN_INFO "sys_tbl[%d] = %0#10x\n", count, table[count]);
2633 }
2634 #endif
2635
2636         return 0;
2637 }
2638
2639
2640 /*
2641  *      Run time support routines
2642  */
2643  
2644 /*
2645  *      Generic "post and forget" helpers. This is less efficient - we do
2646  *      a memcpy for example that isnt strictly needed, but for most uses
2647  *      this is simply not worth optimising
2648  */
2649
2650 int i2o_post_this(struct i2o_controller *c, u32 *data, int len)
2651 {
2652         u32 m;
2653         u32 *msg;
2654         unsigned long t=jiffies;
2655
2656         do
2657         {
2658                 mb();
2659                 m = I2O_POST_READ32(c);
2660         }
2661         while(m==0xFFFFFFFF && (jiffies-t)<HZ);
2662         
2663         if(m==0xFFFFFFFF)
2664         {
2665                 printk(KERN_ERR "%s: Timeout waiting for message frame!\n",
2666                        c->name);
2667                 return -ETIMEDOUT;
2668         }
2669         msg = (u32 *)(c->mem_offset + m);
2670         memcpy_toio(msg, data, len);
2671         i2o_post_message(c,m);
2672         return 0;
2673 }
2674
2675 /**
2676  *      i2o_post_wait_mem       -       I2O query/reply with DMA buffers
2677  *      @c: controller
2678  *      @msg: message to send
2679  *      @len: length of message
2680  *      @timeout: time in seconds to wait
2681  *      @mem1: attached memory buffer 1
2682  *      @mem2: attached memory buffer 2
2683  *      @phys1: physical address of buffer 1
2684  *      @phys2: physical address of buffer 2
2685  *      @size1: size of buffer 1
2686  *      @size2: size of buffer 2
2687  *
2688  *      This core API allows an OSM to post a message and then be told whether
2689  *      or not the system received a successful reply. 
2690  *
2691  *      If the message times out then the value '-ETIMEDOUT' is returned. This
2692  *      is a special case. In this situation the message may (should) complete
2693  *      at an indefinite time in the future. When it completes it will use the
2694  *      memory buffers attached to the request. If -ETIMEDOUT is returned then
2695  *      the memory buffers must not be freed. Instead the event completion will
2696  *      free them for you. In all other cases the buffers are your problem.
2697  *
2698  *      Pass NULL for unneeded buffers.
2699  */
2700  
2701 int i2o_post_wait_mem(struct i2o_controller *c, u32 *msg, int len, int timeout, void *mem1, void *mem2, dma_addr_t phys1, dma_addr_t phys2, int size1, int size2)
2702 {
2703         DECLARE_WAIT_QUEUE_HEAD(wq_i2o_post);
2704         DECLARE_WAITQUEUE(wait, current);
2705         int complete = 0;
2706         int status;
2707         unsigned long flags = 0;
2708         struct i2o_post_wait_data *wait_data =
2709                 kmalloc(sizeof(struct i2o_post_wait_data), GFP_KERNEL);
2710
2711         if(!wait_data)
2712                 return -ENOMEM;
2713
2714         /*
2715          *      Create a new notification object
2716          */
2717         wait_data->status = &status;
2718         wait_data->complete = &complete;
2719         wait_data->mem[0] = mem1;
2720         wait_data->mem[1] = mem2;
2721         wait_data->phys[0] = phys1;
2722         wait_data->phys[1] = phys2;
2723         wait_data->size[0] = size1;
2724         wait_data->size[1] = size2;
2725         
2726         /* 
2727          *      Queue the event with its unique id
2728          */
2729         spin_lock_irqsave(&post_wait_lock, flags);
2730
2731         wait_data->next = post_wait_queue;
2732         post_wait_queue = wait_data;
2733         wait_data->id = (++post_wait_id) & 0x7fff;
2734         wait_data->wq = &wq_i2o_post;
2735
2736         spin_unlock_irqrestore(&post_wait_lock, flags);
2737
2738         /*
2739          *      Fill in the message id
2740          */
2741          
2742         msg[2] = 0x80000000|(u32)core_context|((u32)wait_data->id<<16);
2743         
2744         /*
2745          *      Post the message to the controller. At some point later it 
2746          *      will return. If we time out before it returns then
2747          *      complete will be zero.  From the point post_this returns
2748          *      the wait_data may have been deleted.
2749          */
2750
2751         add_wait_queue(&wq_i2o_post, &wait);
2752         set_current_state(TASK_INTERRUPTIBLE);
2753         if ((status = i2o_post_this(c, msg, len))==0) {
2754                 schedule_timeout(HZ * timeout);
2755         }  
2756         else
2757         {
2758                 remove_wait_queue(&wq_i2o_post, &wait);
2759                 return -EIO;
2760         }
2761         remove_wait_queue(&wq_i2o_post, &wait);
2762
2763         if(signal_pending(current))
2764                 status = -EINTR;
2765                 
2766         spin_lock_irqsave(&post_wait_lock, flags);
2767         barrier();      /* Be sure we see complete as it is locked */
2768         if(!complete)
2769         {
2770                 /* 
2771                  *      Mark the entry dead. We cannot remove it. This is important.
2772                  *      When it does terminate (which it must do if the controller hasnt
2773                  *      died..) then it will otherwise scribble on stuff.
2774                  *      !complete lets us safely check if the entry is still
2775                  *      allocated and thus we can write into it
2776                  */
2777                 wait_data->wq = NULL;
2778                 status = -ETIMEDOUT;
2779         }
2780         else
2781         {
2782                 /* Debugging check - remove me soon */
2783                 if(status == -ETIMEDOUT)
2784                 {
2785                         printk("TIMEDOUT BUG!\n");
2786                         status = -EIO;
2787                 }
2788         }
2789         /* And the wait_data is not leaked either! */    
2790         spin_unlock_irqrestore(&post_wait_lock, flags);
2791         return status;
2792 }
2793
2794 /**
2795  *      i2o_post_wait           -       I2O query/reply
2796  *      @c: controller
2797  *      @msg: message to send
2798  *      @len: length of message
2799  *      @timeout: time in seconds to wait
2800  *
2801  *      This core API allows an OSM to post a message and then be told whether
2802  *      or not the system received a successful reply. 
2803  */
2804  
2805 int i2o_post_wait(struct i2o_controller *c, u32 *msg, int len, int timeout)
2806 {
2807         return i2o_post_wait_mem(c, msg, len, timeout, NULL, NULL, 0, 0, 0, 0);
2808 }
2809
2810 /*
2811  * i2o_post_wait is completed and we want to wake up the 
2812  * sleeping proccess. Called by core's reply handler.
2813  */
2814
2815 static void i2o_post_wait_complete(struct i2o_controller *c, u32 context, int status)
2816 {
2817         struct i2o_post_wait_data **p1, *q;
2818         unsigned long flags;
2819         
2820         /* 
2821          * We need to search through the post_wait 
2822          * queue to see if the given message is still
2823          * outstanding.  If not, it means that the IOP 
2824          * took longer to respond to the message than we 
2825          * had allowed and timer has already expired.  
2826          * Not much we can do about that except log
2827          * it for debug purposes, increase timeout, and recompile
2828          *
2829          * Lock needed to keep anyone from moving queue pointers 
2830          * around while we're looking through them.
2831          */
2832
2833         spin_lock_irqsave(&post_wait_lock, flags);
2834
2835         for(p1 = &post_wait_queue; *p1!=NULL; p1 = &((*p1)->next)) 
2836         {
2837                 q = (*p1);
2838                 if(q->id == ((context >> 16) & 0x7fff)) {
2839                         /*
2840                          *      Delete it 
2841                          */
2842                          
2843                         *p1 = q->next;
2844                         
2845                         /*
2846                          *      Live or dead ?
2847                          */
2848                          
2849                         if(q->wq)
2850                         {
2851                                 /* Live entry - wakeup and set status */
2852                                 *q->status = status;
2853                                 *q->complete = 1;
2854                                 wake_up(q->wq);
2855                         }
2856                         else
2857                         {
2858                                 /*
2859                                  *      Free resources. Caller is dead
2860                                  */
2861
2862                                 if(q->mem[0])
2863                                         pci_free_consistent(c->pdev, q->size[0], q->mem[0], q->phys[0]);
2864                                 if(q->mem[1])
2865                                         pci_free_consistent(c->pdev, q->size[1], q->mem[1], q->phys[1]);
2866
2867                                 printk(KERN_WARNING "i2o_post_wait event completed after timeout.\n");
2868                         }
2869                         kfree(q);
2870                         spin_unlock(&post_wait_lock);
2871                         return;
2872                 }
2873         }
2874         spin_unlock(&post_wait_lock);
2875
2876         printk(KERN_DEBUG "i2o_post_wait: Bogus reply!\n");
2877 }
2878
2879 /*      Issue UTIL_PARAMS_GET or UTIL_PARAMS_SET
2880  *
2881  *      This function can be used for all UtilParamsGet/Set operations.
2882  *      The OperationList is given in oplist-buffer, 
2883  *      and results are returned in reslist-buffer.
2884  *      Note that the minimum sized reslist is 8 bytes and contains
2885  *      ResultCount, ErrorInfoSize, BlockStatus and BlockSize.
2886  */
2887
2888 int i2o_issue_params(int cmd, struct i2o_controller *iop, int tid, 
2889                 void *oplist, int oplen, void *reslist, int reslen)
2890 {
2891         u32 msg[9]; 
2892         u32 *res32 = (u32*)reslist;
2893         u32 *restmp = (u32*)reslist;
2894         int len = 0;
2895         int i = 0;
2896         int wait_status;
2897         u32 *opmem, *resmem;
2898         dma_addr_t opmem_phys, resmem_phys;
2899         
2900         /* Get DMAable memory */
2901         opmem = pci_alloc_consistent(iop->pdev, oplen, &opmem_phys);
2902         if(opmem == NULL)
2903                 return -ENOMEM;
2904         memcpy(opmem, oplist, oplen);
2905         
2906         resmem = pci_alloc_consistent(iop->pdev, reslen, &resmem_phys);
2907         if(resmem == NULL)
2908         {
2909                 pci_free_consistent(iop->pdev, oplen, opmem, opmem_phys);
2910                 return -ENOMEM;
2911         }
2912         
2913         msg[0] = NINE_WORD_MSG_SIZE | SGL_OFFSET_5;
2914         msg[1] = cmd << 24 | HOST_TID << 12 | tid; 
2915         msg[3] = 0;
2916         msg[4] = 0;
2917         msg[5] = 0x54000000 | oplen;    /* OperationList */
2918         msg[6] = opmem_phys;
2919         msg[7] = 0xD0000000 | reslen;   /* ResultList */
2920         msg[8] = resmem_phys;
2921
2922         wait_status = i2o_post_wait_mem(iop, msg, sizeof(msg), 10, opmem, resmem, opmem_phys, resmem_phys, oplen, reslen);
2923         
2924         /*
2925          *      This only looks like a memory leak - don't "fix" it.    
2926          */
2927         if(wait_status == -ETIMEDOUT)
2928                 return wait_status;
2929
2930         memcpy(reslist, resmem, reslen);
2931         pci_free_consistent(iop->pdev, reslen, resmem, resmem_phys);
2932         pci_free_consistent(iop->pdev, oplen, opmem, opmem_phys);
2933         
2934         /* Query failed */
2935         if(wait_status != 0)
2936                 return wait_status;             
2937         /*
2938          * Calculate number of bytes of Result LIST
2939          * We need to loop through each Result BLOCK and grab the length
2940          */
2941         restmp = res32 + 1;
2942         len = 1;
2943         for(i = 0; i < (res32[0]&0X0000FFFF); i++)
2944         {
2945                 if(restmp[0]&0x00FF0000)        /* BlockStatus != SUCCESS */
2946                 {
2947                         printk(KERN_WARNING "%s - Error:\n  ErrorInfoSize = 0x%02x, " 
2948                                         "BlockStatus = 0x%02x, BlockSize = 0x%04x\n",
2949                                         (cmd == I2O_CMD_UTIL_PARAMS_SET) ? "PARAMS_SET"
2950                                         : "PARAMS_GET",   
2951                                         res32[1]>>24, (res32[1]>>16)&0xFF, res32[1]&0xFFFF);
2952         
2953                         /*
2954                          *      If this is the only request,than we return an error
2955                          */
2956                         if((res32[0]&0x0000FFFF) == 1)
2957                         {
2958                                 return -((res32[1] >> 16) & 0xFF); /* -BlockStatus */
2959                         }
2960                 }
2961                 len += restmp[0] & 0x0000FFFF;  /* Length of res BLOCK */
2962                 restmp += restmp[0] & 0x0000FFFF;       /* Skip to next BLOCK */
2963         }
2964         return (len << 2);  /* bytes used by result list */
2965 }
2966
2967 /*
2968  *       Query one scalar group value or a whole scalar group.
2969  */                     
2970 int i2o_query_scalar(struct i2o_controller *iop, int tid, 
2971                      int group, int field, void *buf, int buflen)
2972 {
2973         u16 opblk[] = { 1, 0, I2O_PARAMS_FIELD_GET, group, 1, field };
2974         u8  resblk[8+buflen]; /* 8 bytes for header */
2975         int size;
2976
2977         if (field == -1)                /* whole group */
2978                 opblk[4] = -1;
2979               
2980         size = i2o_issue_params(I2O_CMD_UTIL_PARAMS_GET, iop, tid, 
2981                 opblk, sizeof(opblk), resblk, sizeof(resblk));
2982                 
2983         memcpy(buf, resblk+8, buflen);  /* cut off header */
2984         
2985         if(size>buflen)
2986                 return buflen;
2987         return size;
2988 }
2989
2990 /*
2991  *      Set a scalar group value or a whole group.
2992  */
2993 int i2o_set_scalar(struct i2o_controller *iop, int tid, 
2994                    int group, int field, void *buf, int buflen)
2995 {
2996         u16 *opblk;
2997         u8  resblk[8+buflen]; /* 8 bytes for header */
2998         int size;
2999
3000         opblk = kmalloc(buflen+64, GFP_KERNEL);
3001         if (opblk == NULL)
3002         {
3003                 printk(KERN_ERR "i2o: no memory for operation buffer.\n");
3004                 return -ENOMEM;
3005         }
3006
3007         opblk[0] = 1;                        /* operation count */
3008         opblk[1] = 0;                        /* pad */
3009         opblk[2] = I2O_PARAMS_FIELD_SET;
3010         opblk[3] = group;
3011
3012         if(field == -1) {               /* whole group */
3013                 opblk[4] = -1;
3014                 memcpy(opblk+5, buf, buflen);
3015         }
3016         else                            /* single field */
3017         {
3018                 opblk[4] = 1;
3019                 opblk[5] = field;
3020                 memcpy(opblk+6, buf, buflen);
3021         }   
3022
3023         size = i2o_issue_params(I2O_CMD_UTIL_PARAMS_SET, iop, tid, 
3024                                 opblk, 12+buflen, resblk, sizeof(resblk));
3025
3026         kfree(opblk);
3027         if(size>buflen)
3028                 return buflen;
3029         return size;
3030 }
3031
3032 /* 
3033  *      if oper == I2O_PARAMS_TABLE_GET, get from all rows 
3034  *              if fieldcount == -1 return all fields
3035  *                      ibuf and ibuflen are unused (use NULL, 0)
3036  *              else return specific fields
3037  *                      ibuf contains fieldindexes
3038  *
3039  *      if oper == I2O_PARAMS_LIST_GET, get from specific rows
3040  *              if fieldcount == -1 return all fields
3041  *                      ibuf contains rowcount, keyvalues
3042  *              else return specific fields
3043  *                      fieldcount is # of fieldindexes
3044  *                      ibuf contains fieldindexes, rowcount, keyvalues
3045  *
3046  *      You could also use directly function i2o_issue_params().
3047  */
3048 int i2o_query_table(int oper, struct i2o_controller *iop, int tid, int group,
3049                 int fieldcount, void *ibuf, int ibuflen,
3050                 void *resblk, int reslen) 
3051 {
3052         u16 *opblk;
3053         int size;
3054
3055         opblk = kmalloc(10 + ibuflen, GFP_KERNEL);
3056         if (opblk == NULL)
3057         {
3058                 printk(KERN_ERR "i2o: no memory for query buffer.\n");
3059                 return -ENOMEM;
3060         }
3061
3062         opblk[0] = 1;                           /* operation count */
3063         opblk[1] = 0;                           /* pad */
3064         opblk[2] = oper;
3065         opblk[3] = group;               
3066         opblk[4] = fieldcount;
3067         memcpy(opblk+5, ibuf, ibuflen);         /* other params */
3068
3069         size = i2o_issue_params(I2O_CMD_UTIL_PARAMS_GET,iop, tid, 
3070                                 opblk, 10+ibuflen, resblk, reslen);
3071
3072         kfree(opblk);
3073         if(size>reslen)
3074                 return reslen;
3075         return size;
3076 }
3077
3078 /*
3079  *      Clear table group, i.e. delete all rows.
3080  */
3081 int i2o_clear_table(struct i2o_controller *iop, int tid, int group)
3082 {
3083         u16 opblk[] = { 1, 0, I2O_PARAMS_TABLE_CLEAR, group };
3084         u8  resblk[32]; /* min 8 bytes for result header */
3085
3086         return i2o_issue_params(I2O_CMD_UTIL_PARAMS_SET, iop, tid, 
3087                                 opblk, sizeof(opblk), resblk, sizeof(resblk));
3088 }
3089
3090 /*
3091  *      Add a new row into a table group.
3092  *
3093  *      if fieldcount==-1 then we add whole rows
3094  *              buf contains rowcount, keyvalues
3095  *      else just specific fields are given, rest use defaults
3096  *              buf contains fieldindexes, rowcount, keyvalues
3097  */     
3098 int i2o_row_add_table(struct i2o_controller *iop, int tid,
3099                     int group, int fieldcount, void *buf, int buflen)
3100 {
3101         u16 *opblk;
3102         u8  resblk[32]; /* min 8 bytes for header */
3103         int size;
3104
3105         opblk = kmalloc(buflen+64, GFP_KERNEL);
3106         if (opblk == NULL)
3107         {
3108                 printk(KERN_ERR "i2o: no memory for operation buffer.\n");
3109                 return -ENOMEM;
3110         }
3111
3112         opblk[0] = 1;                   /* operation count */
3113         opblk[1] = 0;                   /* pad */
3114         opblk[2] = I2O_PARAMS_ROW_ADD;
3115         opblk[3] = group;       
3116         opblk[4] = fieldcount;
3117         memcpy(opblk+5, buf, buflen);
3118
3119         size = i2o_issue_params(I2O_CMD_UTIL_PARAMS_SET, iop, tid, 
3120                                 opblk, 10+buflen, resblk, sizeof(resblk));
3121
3122         kfree(opblk);
3123         if(size>buflen)
3124                 return buflen;
3125         return size;
3126 }
3127
3128
3129 /*
3130  * Used for error reporting/debugging purposes.
3131  * Following fail status are common to all classes.
3132  * The preserved message must be handled in the reply handler. 
3133  */
3134 void i2o_report_fail_status(u8 req_status, u32* msg)
3135 {
3136         static char *FAIL_STATUS[] = { 
3137                 "0x80",                         /* not used */
3138                 "SERVICE_SUSPENDED",            /* 0x81 */
3139                 "SERVICE_TERMINATED",           /* 0x82 */
3140                 "CONGESTION",
3141                 "FAILURE",
3142                 "STATE_ERROR",
3143                 "TIME_OUT",
3144                 "ROUTING_FAILURE",
3145                 "INVALID_VERSION",
3146                 "INVALID_OFFSET",
3147                 "INVALID_MSG_FLAGS",
3148                 "FRAME_TOO_SMALL",
3149                 "FRAME_TOO_LARGE",
3150                 "INVALID_TARGET_ID",
3151                 "INVALID_INITIATOR_ID",
3152                 "INVALID_INITIATOR_CONTEX",     /* 0x8F */
3153                 "UNKNOWN_FAILURE"               /* 0xFF */
3154         };
3155
3156         if (req_status == I2O_FSC_TRANSPORT_UNKNOWN_FAILURE)
3157                 printk("TRANSPORT_UNKNOWN_FAILURE (%0#2x)\n.", req_status);
3158         else
3159                 printk("TRANSPORT_%s.\n", FAIL_STATUS[req_status & 0x0F]);
3160
3161         /* Dump some details */
3162
3163         printk(KERN_ERR "  InitiatorId = %d, TargetId = %d\n",
3164                 (msg[1] >> 12) & 0xFFF, msg[1] & 0xFFF); 
3165         printk(KERN_ERR "  LowestVersion = 0x%02X, HighestVersion = 0x%02X\n",
3166                 (msg[4] >> 8) & 0xFF, msg[4] & 0xFF);
3167         printk(KERN_ERR "  FailingHostUnit = 0x%04X,  FailingIOP = 0x%03X\n",
3168                 msg[5] >> 16, msg[5] & 0xFFF);
3169
3170         printk(KERN_ERR "  Severity:  0x%02X ", (msg[4] >> 16) & 0xFF); 
3171         if (msg[4] & (1<<16))
3172                 printk("(FormatError), "
3173                         "this msg can never be delivered/processed.\n");
3174         if (msg[4] & (1<<17))
3175                 printk("(PathError), "
3176                         "this msg can no longer be delivered/processed.\n");
3177         if (msg[4] & (1<<18))
3178                 printk("(PathState), "
3179                         "the system state does not allow delivery.\n");
3180         if (msg[4] & (1<<19))
3181                 printk("(Congestion), resources temporarily not available;"
3182                         "do not retry immediately.\n");
3183 }
3184
3185 /*
3186  * Used for error reporting/debugging purposes.
3187  * Following reply status are common to all classes.
3188  */
3189 void i2o_report_common_status(u8 req_status)
3190 {
3191         static char *REPLY_STATUS[] = { 
3192                 "SUCCESS", 
3193                 "ABORT_DIRTY", 
3194                 "ABORT_NO_DATA_TRANSFER",
3195                 "ABORT_PARTIAL_TRANSFER",
3196                 "ERROR_DIRTY",
3197                 "ERROR_NO_DATA_TRANSFER",
3198                 "ERROR_PARTIAL_TRANSFER",
3199                 "PROCESS_ABORT_DIRTY",
3200                 "PROCESS_ABORT_NO_DATA_TRANSFER",
3201                 "PROCESS_ABORT_PARTIAL_TRANSFER",
3202                 "TRANSACTION_ERROR",
3203                 "PROGRESS_REPORT"       
3204         };
3205
3206         if (req_status >= ARRAY_SIZE(REPLY_STATUS))
3207                 printk("RequestStatus = %0#2x", req_status);
3208         else
3209                 printk("%s", REPLY_STATUS[req_status]);
3210 }
3211
3212 /*
3213  * Used for error reporting/debugging purposes.
3214  * Following detailed status are valid  for executive class, 
3215  * utility class, DDM class and for transaction error replies.
3216  */
3217 static void i2o_report_common_dsc(u16 detailed_status)
3218 {
3219         static char *COMMON_DSC[] = { 
3220                 "SUCCESS",
3221                 "0x01",                         // not used
3222                 "BAD_KEY",
3223                 "TCL_ERROR",
3224                 "REPLY_BUFFER_FULL",
3225                 "NO_SUCH_PAGE",
3226                 "INSUFFICIENT_RESOURCE_SOFT",
3227                 "INSUFFICIENT_RESOURCE_HARD",
3228                 "0x08",                         // not used
3229                 "CHAIN_BUFFER_TOO_LARGE",
3230                 "UNSUPPORTED_FUNCTION",
3231                 "DEVICE_LOCKED",
3232                 "DEVICE_RESET",
3233                 "INAPPROPRIATE_FUNCTION",
3234                 "INVALID_INITIATOR_ADDRESS",
3235                 "INVALID_MESSAGE_FLAGS",
3236                 "INVALID_OFFSET",
3237                 "INVALID_PARAMETER",
3238                 "INVALID_REQUEST",
3239                 "INVALID_TARGET_ADDRESS",
3240                 "MESSAGE_TOO_LARGE",
3241                 "MESSAGE_TOO_SMALL",
3242                 "MISSING_PARAMETER",
3243                 "TIMEOUT",
3244                 "UNKNOWN_ERROR",
3245                 "UNKNOWN_FUNCTION",
3246                 "UNSUPPORTED_VERSION",
3247                 "DEVICE_BUSY",
3248                 "DEVICE_NOT_AVAILABLE"          
3249         };
3250
3251         if (detailed_status > I2O_DSC_DEVICE_NOT_AVAILABLE)
3252                 printk(" / DetailedStatus = %0#4x.\n", detailed_status);
3253         else
3254                 printk(" / %s.\n", COMMON_DSC[detailed_status]);
3255 }
3256
3257 /*
3258  * Used for error reporting/debugging purposes
3259  */
3260 static void i2o_report_lan_dsc(u16 detailed_status)
3261 {
3262         static char *LAN_DSC[] = {      // Lan detailed status code strings
3263                 "SUCCESS",
3264                 "DEVICE_FAILURE",
3265                 "DESTINATION_NOT_FOUND",
3266                 "TRANSMIT_ERROR",
3267                 "TRANSMIT_ABORTED",
3268                 "RECEIVE_ERROR",
3269                 "RECEIVE_ABORTED",
3270                 "DMA_ERROR",
3271                 "BAD_PACKET_DETECTED",
3272                 "OUT_OF_MEMORY",
3273                 "BUCKET_OVERRUN",
3274                 "IOP_INTERNAL_ERROR",
3275                 "CANCELED",
3276                 "INVALID_TRANSACTION_CONTEXT",
3277                 "DEST_ADDRESS_DETECTED",
3278                 "DEST_ADDRESS_OMITTED",
3279                 "PARTIAL_PACKET_RETURNED",
3280                 "TEMP_SUSPENDED_STATE", // last Lan detailed status code
3281                 "INVALID_REQUEST"       // general detailed status code
3282         };
3283
3284         if (detailed_status > I2O_DSC_INVALID_REQUEST)
3285                 printk(" / %0#4x.\n", detailed_status);
3286         else
3287                 printk(" / %s.\n", LAN_DSC[detailed_status]);
3288 }
3289
3290 /*
3291  * Used for error reporting/debugging purposes
3292  */
3293 static void i2o_report_util_cmd(u8 cmd)
3294 {
3295         switch (cmd) {
3296         case I2O_CMD_UTIL_NOP:
3297                 printk("UTIL_NOP, ");
3298                 break;                  
3299         case I2O_CMD_UTIL_ABORT:
3300                 printk("UTIL_ABORT, ");
3301                 break;
3302         case I2O_CMD_UTIL_CLAIM:
3303                 printk("UTIL_CLAIM, ");
3304                 break;
3305         case I2O_CMD_UTIL_RELEASE:
3306                 printk("UTIL_CLAIM_RELEASE, ");
3307                 break;
3308         case I2O_CMD_UTIL_CONFIG_DIALOG:
3309                 printk("UTIL_CONFIG_DIALOG, ");
3310                 break;
3311         case I2O_CMD_UTIL_DEVICE_RESERVE:
3312                 printk("UTIL_DEVICE_RESERVE, ");
3313                 break;
3314         case I2O_CMD_UTIL_DEVICE_RELEASE:
3315                 printk("UTIL_DEVICE_RELEASE, ");
3316                 break;
3317         case I2O_CMD_UTIL_EVT_ACK:
3318                 printk("UTIL_EVENT_ACKNOWLEDGE, ");
3319                 break;
3320         case I2O_CMD_UTIL_EVT_REGISTER:
3321                 printk("UTIL_EVENT_REGISTER, ");
3322                 break;
3323         case I2O_CMD_UTIL_LOCK:
3324                 printk("UTIL_LOCK, ");
3325                 break;
3326         case I2O_CMD_UTIL_LOCK_RELEASE:
3327                 printk("UTIL_LOCK_RELEASE, ");
3328                 break;
3329         case I2O_CMD_UTIL_PARAMS_GET:
3330                 printk("UTIL_PARAMS_GET, ");
3331                 break;
3332         case I2O_CMD_UTIL_PARAMS_SET:
3333                 printk("UTIL_PARAMS_SET, ");
3334                 break;
3335         case I2O_CMD_UTIL_REPLY_FAULT_NOTIFY:
3336                 printk("UTIL_REPLY_FAULT_NOTIFY, ");
3337                 break;
3338         default:
3339                 printk("Cmd = %0#2x, ",cmd);    
3340         }
3341 }
3342
3343 /*
3344  * Used for error reporting/debugging purposes
3345  */
3346 static void i2o_report_exec_cmd(u8 cmd)
3347 {
3348         switch (cmd) {
3349         case I2O_CMD_ADAPTER_ASSIGN:
3350                 printk("EXEC_ADAPTER_ASSIGN, ");
3351                 break;
3352         case I2O_CMD_ADAPTER_READ:
3353                 printk("EXEC_ADAPTER_READ, ");
3354                 break;
3355         case I2O_CMD_ADAPTER_RELEASE:
3356                 printk("EXEC_ADAPTER_RELEASE, ");
3357                 break;
3358         case I2O_CMD_BIOS_INFO_SET:
3359                 printk("EXEC_BIOS_INFO_SET, ");
3360                 break;
3361         case I2O_CMD_BOOT_DEVICE_SET:
3362                 printk("EXEC_BOOT_DEVICE_SET, ");
3363                 break;
3364         case I2O_CMD_CONFIG_VALIDATE:
3365                 printk("EXEC_CONFIG_VALIDATE, ");
3366                 break;
3367         case I2O_CMD_CONN_SETUP:
3368                 printk("EXEC_CONN_SETUP, ");
3369                 break;
3370         case I2O_CMD_DDM_DESTROY:
3371                 printk("EXEC_DDM_DESTROY, ");
3372                 break;
3373         case I2O_CMD_DDM_ENABLE:
3374                 printk("EXEC_DDM_ENABLE, ");
3375                 break;
3376         case I2O_CMD_DDM_QUIESCE:
3377                 printk("EXEC_DDM_QUIESCE, ");
3378                 break;
3379         case I2O_CMD_DDM_RESET:
3380                 printk("EXEC_DDM_RESET, ");
3381                 break;
3382         case I2O_CMD_DDM_SUSPEND:
3383                 printk("EXEC_DDM_SUSPEND, ");
3384                 break;
3385         case I2O_CMD_DEVICE_ASSIGN:
3386                 printk("EXEC_DEVICE_ASSIGN, ");
3387                 break;
3388         case I2O_CMD_DEVICE_RELEASE:
3389                 printk("EXEC_DEVICE_RELEASE, ");
3390                 break;
3391         case I2O_CMD_HRT_GET:
3392                 printk("EXEC_HRT_GET, ");
3393                 break;
3394         case I2O_CMD_ADAPTER_CLEAR:
3395                 printk("EXEC_IOP_CLEAR, ");
3396                 break;
3397         case I2O_CMD_ADAPTER_CONNECT:
3398                 printk("EXEC_IOP_CONNECT, ");
3399                 break;
3400         case I2O_CMD_ADAPTER_RESET:
3401                 printk("EXEC_IOP_RESET, ");
3402                 break;
3403         case I2O_CMD_LCT_NOTIFY:
3404                 printk("EXEC_LCT_NOTIFY, ");
3405                 break;
3406         case I2O_CMD_OUTBOUND_INIT:
3407                 printk("EXEC_OUTBOUND_INIT, ");
3408                 break;
3409         case I2O_CMD_PATH_ENABLE:
3410                 printk("EXEC_PATH_ENABLE, ");
3411                 break;
3412         case I2O_CMD_PATH_QUIESCE:
3413                 printk("EXEC_PATH_QUIESCE, ");
3414                 break;
3415         case I2O_CMD_PATH_RESET:
3416                 printk("EXEC_PATH_RESET, ");
3417                 break;
3418         case I2O_CMD_STATIC_MF_CREATE:
3419                 printk("EXEC_STATIC_MF_CREATE, ");
3420                 break;
3421         case I2O_CMD_STATIC_MF_RELEASE:
3422                 printk("EXEC_STATIC_MF_RELEASE, ");
3423                 break;
3424         case I2O_CMD_STATUS_GET:
3425                 printk("EXEC_STATUS_GET, ");
3426                 break;
3427         case I2O_CMD_SW_DOWNLOAD:
3428                 printk("EXEC_SW_DOWNLOAD, ");
3429                 break;
3430         case I2O_CMD_SW_UPLOAD:
3431                 printk("EXEC_SW_UPLOAD, ");
3432                 break;
3433         case I2O_CMD_SW_REMOVE:
3434                 printk("EXEC_SW_REMOVE, ");
3435                 break;
3436         case I2O_CMD_SYS_ENABLE:
3437                 printk("EXEC_SYS_ENABLE, ");
3438                 break;
3439         case I2O_CMD_SYS_MODIFY:
3440                 printk("EXEC_SYS_MODIFY, ");
3441                 break;
3442         case I2O_CMD_SYS_QUIESCE:
3443                 printk("EXEC_SYS_QUIESCE, ");
3444                 break;
3445         case I2O_CMD_SYS_TAB_SET:
3446                 printk("EXEC_SYS_TAB_SET, ");
3447                 break;
3448         default:
3449                 printk("Cmd = %#02x, ",cmd);    
3450         }
3451 }
3452
3453 /*
3454  * Used for error reporting/debugging purposes
3455  */
3456 static void i2o_report_lan_cmd(u8 cmd)
3457 {
3458         switch (cmd) {
3459         case LAN_PACKET_SEND:
3460                 printk("LAN_PACKET_SEND, "); 
3461                 break;
3462         case LAN_SDU_SEND:
3463                 printk("LAN_SDU_SEND, ");
3464                 break;
3465         case LAN_RECEIVE_POST:
3466                 printk("LAN_RECEIVE_POST, ");
3467                 break;
3468         case LAN_RESET:
3469                 printk("LAN_RESET, ");
3470                 break;
3471         case LAN_SUSPEND:
3472                 printk("LAN_SUSPEND, ");
3473                 break;
3474         default:
3475                 printk("Cmd = %0#2x, ",cmd);    
3476         }       
3477 }
3478
3479 /*
3480  * Used for error reporting/debugging purposes.
3481  * Report Cmd name, Request status, Detailed Status.
3482  */
3483 void i2o_report_status(const char *severity, const char *str, u32 *msg)
3484 {
3485         u8 cmd = (msg[1]>>24)&0xFF;
3486         u8 req_status = (msg[4]>>24)&0xFF;
3487         u16 detailed_status = msg[4]&0xFFFF;
3488         struct i2o_handler *h = i2o_handlers[msg[2] & (MAX_I2O_MODULES-1)];
3489
3490         if (cmd == I2O_CMD_UTIL_EVT_REGISTER)
3491                 return;                         // No status in this reply
3492
3493         printk("%s%s: ", severity, str);
3494
3495         if (cmd < 0x1F)                         // Utility cmd
3496                 i2o_report_util_cmd(cmd);
3497         
3498         else if (cmd >= 0xA0 && cmd <= 0xEF)    // Executive cmd
3499                 i2o_report_exec_cmd(cmd);
3500         
3501         else if (h->class == I2O_CLASS_LAN && cmd >= 0x30 && cmd <= 0x3F)
3502                 i2o_report_lan_cmd(cmd);        // LAN cmd
3503         else
3504                 printk("Cmd = %0#2x, ", cmd);   // Other cmds
3505
3506         if (msg[0] & MSG_FAIL) {
3507                 i2o_report_fail_status(req_status, msg);
3508                 return;
3509         }
3510         
3511         i2o_report_common_status(req_status);
3512
3513         if (cmd < 0x1F || (cmd >= 0xA0 && cmd <= 0xEF))
3514                 i2o_report_common_dsc(detailed_status); 
3515         else if (h->class == I2O_CLASS_LAN && cmd >= 0x30 && cmd <= 0x3F)
3516                 i2o_report_lan_dsc(detailed_status);
3517         else
3518                 printk(" / DetailedStatus = %0#4x.\n", detailed_status); 
3519 }
3520
3521 /* Used to dump a message to syslog during debugging */
3522 void i2o_dump_message(u32 *msg)
3523 {
3524 #ifdef DRIVERDEBUG
3525         int i;
3526         printk(KERN_INFO "Dumping I2O message size %d @ %p\n", 
3527                 msg[0]>>16&0xffff, msg);
3528         for(i = 0; i < ((msg[0]>>16)&0xffff); i++)
3529                 printk(KERN_INFO "  msg[%d] = %0#10x\n", i, msg[i]);
3530 #endif
3531 }
3532
3533 /*
3534  * I2O reboot/shutdown notification.
3535  *
3536  * - Call each OSM's reboot notifier (if one exists)
3537  * - Quiesce each IOP in the system
3538  *
3539  * Each IOP has to be quiesced before we can ensure that the system
3540  * can be properly shutdown as a transaction that has already been
3541  * acknowledged still needs to be placed in permanent store on the IOP.
3542  * The SysQuiesce causes the IOP to force all HDMs to complete their
3543  * transactions before returning, so only at that point is it safe
3544  * 
3545  */
3546 static int i2o_reboot_event(struct notifier_block *n, unsigned long code, void
3547 *p)
3548 {
3549         int i = 0;
3550         struct i2o_controller *c = NULL;
3551
3552         if(code != SYS_RESTART && code != SYS_HALT && code != SYS_POWER_OFF)
3553                 return NOTIFY_DONE;
3554
3555         printk(KERN_INFO "Shutting down I2O system.\n");
3556         printk(KERN_INFO 
3557                 "   This could take a few minutes if there are many devices attached\n");
3558
3559         for(i = 0; i < MAX_I2O_MODULES; i++)
3560         {
3561                 if(i2o_handlers[i] && i2o_handlers[i]->reboot_notify)
3562                         i2o_handlers[i]->reboot_notify();
3563         }
3564
3565         for(c = i2o_controller_chain; c; c = c->next)
3566         {
3567                 if(i2o_quiesce_controller(c))
3568                 {
3569                         printk(KERN_WARNING "i2o: Could not quiesce %s.\n"
3570                                "Verify setup on next system power up.\n",
3571                                c->name);
3572                 }
3573         }
3574
3575         printk(KERN_INFO "I2O system down.\n");
3576         return NOTIFY_DONE;
3577 }
3578
3579
3580
3581
3582 /**
3583  *      i2o_pci_dispose         -       Free bus specific resources
3584  *      @c: I2O controller
3585  *
3586  *      Disable interrupts and then free interrupt, I/O and mtrr resources 
3587  *      used by this controller. Called by the I2O core on unload.
3588  */
3589  
3590 static void i2o_pci_dispose(struct i2o_controller *c)
3591 {
3592         I2O_IRQ_WRITE32(c,0xFFFFFFFF);
3593         if(c->irq > 0)
3594                 free_irq(c->irq, c);
3595         iounmap(((u8 *)c->post_port)-0x40);
3596
3597 #ifdef CONFIG_MTRR
3598         if(c->mtrr_reg0 > 0)
3599                 mtrr_del(c->mtrr_reg0, 0, 0);
3600         if(c->mtrr_reg1 > 0)
3601                 mtrr_del(c->mtrr_reg1, 0, 0);
3602 #endif
3603 }
3604
3605 /**
3606  *      i2o_pci_interrupt       -       Bus specific interrupt handler
3607  *      @irq: interrupt line
3608  *      @dev_id: cookie
3609  *
3610  *      Handle an interrupt from a PCI based I2O controller. This turns out
3611  *      to be rather simple. We keep the controller pointer in the cookie.
3612  */
3613  
3614 static irqreturn_t i2o_pci_interrupt(int irq, void *dev_id, struct pt_regs *r)
3615 {
3616         struct i2o_controller *c = dev_id;
3617         i2o_run_queue(c);
3618         return IRQ_HANDLED;
3619 }       
3620
3621 /**
3622  *      i2o_pci_install         -       Install a PCI i2o controller
3623  *      @dev: PCI device of the I2O controller
3624  *
3625  *      Install a PCI (or in theory AGP) i2o controller. Devices are
3626  *      initialized, configured and registered with the i2o core subsystem. Be
3627  *      very careful with ordering. There may be pending interrupts.
3628  *
3629  *      To Do: Add support for polled controllers
3630  */
3631
3632 int __init i2o_pci_install(struct pci_dev *dev)
3633 {
3634         struct i2o_controller *c=kmalloc(sizeof(struct i2o_controller),
3635                                                 GFP_KERNEL);
3636         unsigned long mem;
3637         u32 memptr = 0;
3638         u32 size;
3639         
3640         int i;
3641
3642         if(c==NULL)
3643         {
3644                 printk(KERN_ERR "i2o: Insufficient memory to add controller.\n");
3645                 return -ENOMEM;
3646         }
3647         memset(c, 0, sizeof(*c));
3648
3649         for(i=0; i<6; i++)
3650         {
3651                 /* Skip I/O spaces */
3652                 if(!(pci_resource_flags(dev, i) & IORESOURCE_IO))
3653                 {
3654                         memptr = pci_resource_start(dev, i);
3655                         break;
3656                 }
3657         }
3658         
3659         if(i==6)
3660         {
3661                 printk(KERN_ERR "i2o: I2O controller has no memory regions defined.\n");
3662                 kfree(c);
3663                 return -EINVAL;
3664         }
3665         
3666         size = dev->resource[i].end-dev->resource[i].start+1;   
3667         /* Map the I2O controller */
3668         
3669         printk(KERN_INFO "i2o: PCI I2O controller at 0x%08X size=%d\n", memptr, size);
3670         mem = (unsigned long)ioremap(memptr, size);
3671         if(mem==0)
3672         {
3673                 printk(KERN_ERR "i2o: Unable to map controller.\n");
3674                 kfree(c);
3675                 return -EINVAL;
3676         }
3677
3678         c->irq = -1;
3679         c->dpt = 0;
3680         c->short_req = 0;
3681         c->pdev = dev;
3682
3683 #if BITS_PER_LONG == 64
3684         c->context_list_lock = SPIN_LOCK_UNLOCKED;
3685 #endif
3686
3687         c->irq_mask = mem+0x34;
3688         c->post_port = mem+0x40;
3689         c->reply_port = mem+0x44;
3690
3691         c->mem_phys = memptr;
3692         c->mem_offset = mem;
3693         
3694         /*
3695          *      Cards that fall apart if you hit them with large I/O
3696          *      loads...
3697          */
3698          
3699         if(dev->vendor == PCI_VENDOR_ID_NCR && dev->device == 0x0630)
3700         {
3701                 c->short_req = 1;
3702                 printk(KERN_INFO "I2O: Symbios FC920 workarounds activated.\n");
3703         }
3704         if(dev->subsystem_vendor == PCI_VENDOR_ID_PROMISE)
3705         {
3706                 c->promise = 1;
3707                 printk(KERN_INFO "I2O: Promise workarounds activated.\n");
3708         }
3709
3710         /*
3711          *      Cards that go bananas if you quiesce them before you reset
3712          *      them
3713          */
3714          
3715         if(dev->vendor == PCI_VENDOR_ID_DPT)
3716                 c->dpt=1;
3717         
3718         /* 
3719          * Enable Write Combining MTRR for IOP's memory region
3720          */
3721 #ifdef CONFIG_MTRR
3722         c->mtrr_reg0 =
3723                 mtrr_add(c->mem_phys, size, MTRR_TYPE_WRCOMB, 1);
3724         /*
3725          * If it is an INTEL i960 I/O processor then set the first 64K to
3726          * Uncacheable since the region contains the Messaging unit which
3727          * shouldn't be cached.
3728          */
3729         c->mtrr_reg1 = -1;
3730         if(dev->vendor == PCI_VENDOR_ID_INTEL || dev->vendor == PCI_VENDOR_ID_DPT)
3731         {
3732                 printk(KERN_INFO "I2O: MTRR workaround for Intel i960 processor\n"); 
3733                 c->mtrr_reg1 =  mtrr_add(c->mem_phys, 65536, MTRR_TYPE_UNCACHABLE, 1);
3734                 if(c->mtrr_reg1< 0)
3735                 {
3736                         printk(KERN_INFO "i2o_pci: Error in setting MTRR_TYPE_UNCACHABLE\n");
3737                         mtrr_del(c->mtrr_reg0, c->mem_phys, size);
3738                         c->mtrr_reg0 = -1;
3739                 }
3740         }
3741
3742 #endif
3743
3744         I2O_IRQ_WRITE32(c,0xFFFFFFFF);
3745
3746         i = i2o_install_controller(c);
3747         
3748         if(i<0)
3749         {
3750                 printk(KERN_ERR "i2o: Unable to install controller.\n");
3751                 kfree(c);
3752                 iounmap((void *)mem);
3753                 return i;
3754         }
3755
3756         c->irq = dev->irq;
3757         if(c->irq)
3758         {
3759                 i=request_irq(dev->irq, i2o_pci_interrupt, SA_SHIRQ,
3760                         c->name, c);
3761                 if(i<0)
3762                 {
3763                         printk(KERN_ERR "%s: unable to allocate interrupt %d.\n",
3764                                 c->name, dev->irq);
3765                         c->irq = -1;
3766                         i2o_delete_controller(c);
3767                         iounmap((void *)mem);
3768                         return -EBUSY;
3769                 }
3770         }
3771
3772         printk(KERN_INFO "%s: Installed at IRQ%d\n", c->name, dev->irq);
3773         I2O_IRQ_WRITE32(c,0x0);
3774         c->enabled = 1;
3775         return 0;       
3776 }
3777
3778 /**
3779  *      i2o_pci_scan    -       Scan the pci bus for controllers
3780  *      
3781  *      Scan the PCI devices on the system looking for any device which is a 
3782  *      memory of the Intelligent, I2O class. We attempt to set up each such device
3783  *      and register it with the core.
3784  *
3785  *      Returns the number of controllers registered
3786  *
3787  *      Note; Do not change this to a hot plug interface. I2O 1.5 itself
3788  *      does not support hot plugging.
3789  */
3790  
3791 int __init i2o_pci_scan(void)
3792 {
3793         struct pci_dev *dev = NULL;
3794         int count=0;
3795         
3796         printk(KERN_INFO "i2o: Checking for PCI I2O controllers...\n");
3797
3798         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL)
3799         {
3800                 if((dev->class>>8)!=PCI_CLASS_INTELLIGENT_I2O)
3801                         continue;
3802
3803                 if((dev->class&0xFF)>1)
3804                 {
3805                         printk(KERN_INFO "i2o: I2O Controller found but does not support I2O 1.5 (skipping).\n");
3806                         continue;
3807                 }
3808                 if (pci_enable_device(dev))
3809                         continue;
3810                 printk(KERN_INFO "i2o: I2O controller on bus %d at %d.\n",
3811                         dev->bus->number, dev->devfn);
3812                 if(pci_set_dma_mask(dev, 0xffffffff))
3813                 {
3814                         printk(KERN_WARNING "I2O controller on bus %d at %d : No suitable DMA available\n", dev->bus->number, dev->devfn);
3815                         continue;
3816                 }
3817                 pci_set_master(dev);
3818                 if(i2o_pci_install(dev)==0)
3819                         count++;
3820         }
3821         if(count)
3822                 printk(KERN_INFO "i2o: %d I2O controller%s found and installed.\n", count,
3823                         count==1?"":"s");
3824         return count?count:-ENODEV;
3825 }
3826
3827 static int i2o_core_init(void)
3828 {
3829         printk(KERN_INFO "I2O Core - (C) Copyright 1999 Red Hat Software\n");
3830         if (i2o_install_handler(&i2o_core_handler) < 0)
3831         {
3832                 printk(KERN_ERR "i2o_core: Unable to install core handler.\nI2O stack not loaded!");
3833                 return 0;
3834         }
3835
3836         core_context = i2o_core_handler.context;
3837
3838         /*
3839          * Initialize event handling thread
3840          */     
3841
3842         init_MUTEX_LOCKED(&evt_sem);
3843         evt_pid = kernel_thread(i2o_core_evt, &evt_reply, CLONE_SIGHAND);
3844         if(evt_pid < 0)
3845         {
3846                 printk(KERN_ERR "I2O: Could not create event handler kernel thread\n");
3847                 i2o_remove_handler(&i2o_core_handler);
3848                 return 0;
3849         }
3850         else
3851                 printk(KERN_INFO "I2O: Event thread created as pid %d\n", evt_pid);
3852
3853         i2o_pci_scan();
3854         if(i2o_num_controllers)
3855                 i2o_sys_init();
3856
3857         register_reboot_notifier(&i2o_reboot_notifier);
3858
3859         return 0;
3860 }
3861
3862 static void i2o_core_exit(void)
3863 {
3864         int stat;
3865
3866         unregister_reboot_notifier(&i2o_reboot_notifier);
3867
3868         if(i2o_num_controllers)
3869                 i2o_sys_shutdown();
3870
3871         /*
3872          * If this is shutdown time, the thread has already been killed
3873          */
3874         if(evt_running) {
3875                 printk("Terminating i2o threads...");
3876                 stat = kill_proc(evt_pid, SIGKILL, 1);
3877                 if(!stat) {
3878                         printk("waiting...\n");
3879                         wait_for_completion(&evt_dead);
3880                 }
3881                 printk("done.\n");
3882         }
3883         i2o_remove_handler(&i2o_core_handler);
3884 }
3885
3886 module_init(i2o_core_init);
3887 module_exit(i2o_core_exit);
3888
3889 MODULE_PARM(verbose, "i");
3890 MODULE_PARM_DESC(verbose, "Verbose diagnostics");
3891
3892 MODULE_AUTHOR("Red Hat Software");
3893 MODULE_DESCRIPTION("I2O Core");
3894 MODULE_LICENSE("GPL");
3895
3896 EXPORT_SYMBOL(i2o_controller_chain);
3897 EXPORT_SYMBOL(i2o_num_controllers);
3898 EXPORT_SYMBOL(i2o_find_controller);
3899 EXPORT_SYMBOL(i2o_unlock_controller);
3900 EXPORT_SYMBOL(i2o_status_get);
3901 EXPORT_SYMBOL(i2o_install_handler);
3902 EXPORT_SYMBOL(i2o_remove_handler);
3903 EXPORT_SYMBOL(i2o_install_controller);
3904 EXPORT_SYMBOL(i2o_delete_controller);
3905 EXPORT_SYMBOL(i2o_run_queue);
3906 EXPORT_SYMBOL(i2o_claim_device);
3907 EXPORT_SYMBOL(i2o_release_device);
3908 EXPORT_SYMBOL(i2o_device_notify_on);
3909 EXPORT_SYMBOL(i2o_device_notify_off);
3910 EXPORT_SYMBOL(i2o_post_this);
3911 EXPORT_SYMBOL(i2o_post_wait);
3912 EXPORT_SYMBOL(i2o_post_wait_mem);
3913 EXPORT_SYMBOL(i2o_query_scalar);
3914 EXPORT_SYMBOL(i2o_set_scalar);
3915 EXPORT_SYMBOL(i2o_query_table);
3916 EXPORT_SYMBOL(i2o_clear_table);
3917 EXPORT_SYMBOL(i2o_row_add_table);
3918 EXPORT_SYMBOL(i2o_issue_params);
3919 EXPORT_SYMBOL(i2o_event_register);
3920 EXPORT_SYMBOL(i2o_event_ack);
3921 EXPORT_SYMBOL(i2o_report_status);
3922 EXPORT_SYMBOL(i2o_dump_message);
3923 EXPORT_SYMBOL(i2o_get_class_name);
3924 EXPORT_SYMBOL(i2o_context_list_add);
3925 EXPORT_SYMBOL(i2o_context_list_get);
3926 EXPORT_SYMBOL(i2o_context_list_remove);