~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/i2o/i2o_core.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.