This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / message / i2o / iop.c
1 /*
2  *      Functions to handle I2O controllers and I2O message handling
3  *
4  *      Copyright (C) 1999-2002 Red Hat Software
5  *
6  *      Written by Alan Cox, Building Number Three Ltd
7  *
8  *      This program is free software; you can redistribute it and/or modify it
9  *      under the terms of the GNU General Public License as published by the
10  *      Free Software Foundation; either version 2 of the License, or (at your
11  *      option) any later version.
12  *
13  *      A lot of the I2O message side code from this is taken from the
14  *      Red Creek RCPCI45 adapter driver by Red Creek Communications
15  *
16  *      Fixes/additions:
17  *              Philipp Rumpf
18  *              Juha Sievänen <Juha.Sievanen@cs.Helsinki.FI>
19  *              Auvo Häkkinen <Auvo.Hakkinen@cs.Helsinki.FI>
20  *              Deepak Saxena <deepak@plexity.net>
21  *              Boji T Kannanthanam <boji.t.kannanthanam@intel.com>
22  *              Alan Cox <alan@redhat.com>:
23  *                      Ported to Linux 2.5.
24  *              Markus Lidel <Markus.Lidel@shadowconnect.com>:
25  *                      Minor fixes for 2.6.
26  */
27
28 #include <linux/module.h>
29 #include <linux/i2o.h>
30
31 /* global I2O controller list */
32 LIST_HEAD(i2o_controllers);
33
34 /*
35  * global I2O System Table. Contains information about all the IOPs in the
36  * system. Used to inform IOPs about each others existence.
37  */
38 static struct i2o_dma i2o_systab;
39
40 /* Module internal functions from other sources */
41 extern struct i2o_driver i2o_exec_driver;
42 extern int i2o_exec_lct_get(struct i2o_controller *);
43 extern void i2o_device_remove(struct i2o_device *);
44
45 extern int __init i2o_driver_init(void);
46 extern void __exit i2o_driver_exit(void);
47 extern int __init i2o_exec_init(void);
48 extern void __exit i2o_exec_exit(void);
49 extern int __init i2o_pci_init(void);
50 extern void __exit i2o_pci_exit(void);
51 extern int i2o_device_init(void);
52 extern void i2o_device_exit(void);
53
54 /**
55  *      i2o_msg_nop - Returns a message which is not used
56  *      @c: I2O controller from which the message was created
57  *      @m: message which should be returned
58  *
59  *      If you fetch a message via i2o_msg_get, and can't use it, you must
60  *      return the message with this function. Otherwise the message frame
61  *      is lost.
62  */
63 void i2o_msg_nop(struct i2o_controller *c, u32 m)
64 {
65         struct i2o_message *msg = c->in_queue.virt + m;
66
67         writel(THREE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
68         writel(I2O_CMD_UTIL_NOP << 24 | HOST_TID << 12 | ADAPTER_TID,
69                &msg->u.head[1]);
70         writel(0, &msg->u.head[2]);
71         writel(0, &msg->u.head[3]);
72         i2o_msg_post(c, m);
73 };
74
75 /**
76  *      i2o_msg_get_wait - obtain an I2O message from the IOP
77  *      @c: I2O controller
78  *      @msg: pointer to a I2O message pointer
79  *      @wait: how long to wait until timeout
80  *
81  *      This function waits up to wait seconds for a message slot to be
82  *      available.
83  *
84  *      On a success the message is returned and the pointer to the message is
85  *      set in msg. The returned message is the physical page frame offset
86  *      address from the read port (see the i2o spec). If no message is
87  *      available returns I2O_QUEUE_EMPTY and msg is leaved untouched.
88  */
89 u32 i2o_msg_get_wait(struct i2o_controller *c, struct i2o_message **msg,
90                      int wait)
91 {
92         unsigned long timeout = jiffies + wait * HZ;
93         u32 m;
94
95         while ((m = i2o_msg_get(c, msg)) == I2O_QUEUE_EMPTY) {
96                 if (time_after(jiffies, timeout)) {
97                         pr_debug("%s: Timeout waiting for message frame.\n",
98                                  c->name);
99                         return I2O_QUEUE_EMPTY;
100                 }
101                 set_current_state(TASK_UNINTERRUPTIBLE);
102                 schedule_timeout(1);
103         }
104
105         return m;
106 };
107
108 #if BITS_PER_LONG == 64
109 /**
110  *      i2o_cntxt_list_add - Append a pointer to context list and return a id
111  *      @c: controller to which the context list belong
112  *      @ptr: pointer to add to the context list
113  *
114  *      Because the context field in I2O is only 32-bit large, on 64-bit the
115  *      pointer is to large to fit in the context field. The i2o_cntxt_list
116  *      functions therefore map pointers to context fields.
117  *
118  *      Returns context id > 0 on success or 0 on failure.
119  */
120 u32 i2o_cntxt_list_add(struct i2o_controller *c, void *ptr)
121 {
122         struct i2o_context_list_element *entry;
123         unsigned long flags;
124
125         if (!ptr)
126                 printk(KERN_ERR "NULL pointer found!\n");
127
128         entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
129         if (!entry) {
130                 printk(KERN_ERR "i2o: Could not allocate memory for context "
131                        "list element\n");
132                 return 0;
133         }
134
135         entry->ptr = ptr;
136         entry->timestamp = jiffies;
137         INIT_LIST_HEAD(&entry->list);
138
139         spin_lock_irqsave(&c->context_list_lock, flags);
140
141         if (unlikely(atomic_inc_and_test(&c->context_list_counter)))
142                 atomic_inc(&c->context_list_counter);
143
144         entry->context = atomic_read(&c->context_list_counter);
145
146         list_add(&entry->list, &c->context_list);
147
148         spin_unlock_irqrestore(&c->context_list_lock, flags);
149
150         pr_debug("Add context to list %p -> %d\n", ptr, context);
151
152         return entry->context;
153 };
154
155 /**
156  *      i2o_cntxt_list_remove - Remove a pointer from the context list
157  *      @c: controller to which the context list belong
158  *      @ptr: pointer which should be removed from the context list
159  *
160  *      Removes a previously added pointer from the context list and returns
161  *      the matching context id.
162  *
163  *      Returns context id on succes or 0 on failure.
164  */
165 u32 i2o_cntxt_list_remove(struct i2o_controller *c, void *ptr)
166 {
167         struct i2o_context_list_element *entry;
168         u32 context = 0;
169         unsigned long flags;
170
171         spin_lock_irqsave(&c->context_list_lock, flags);
172         list_for_each_entry(entry, &c->context_list, list)
173             if (entry->ptr == ptr) {
174                 list_del(&entry->list);
175                 context = entry->context;
176                 kfree(entry);
177                 break;
178         }
179         spin_unlock_irqrestore(&c->context_list_lock, flags);
180
181         if (!context)
182                 printk(KERN_WARNING "i2o: Could not remove nonexistent ptr "
183                        "%p\n", ptr);
184
185         pr_debug("remove ptr from context list %d -> %p\n", context, ptr);
186
187         return context;
188 };
189
190 /**
191  *      i2o_cntxt_list_get - Get a pointer from the context list and remove it
192  *      @c: controller to which the context list belong
193  *      @context: context id to which the pointer belong
194  *
195  *      Returns pointer to the matching context id on success or NULL on
196  *      failure.
197  */
198 void *i2o_cntxt_list_get(struct i2o_controller *c, u32 context)
199 {
200         struct i2o_context_list_element *entry;
201         unsigned long flags;
202         void *ptr = NULL;
203
204         spin_lock_irqsave(&c->context_list_lock, flags);
205         list_for_each_entry(entry, &c->context_list, list)
206             if (entry->context == context) {
207                 list_del(&entry->list);
208                 ptr = entry->ptr;
209                 kfree(entry);
210                 break;
211         }
212         spin_unlock_irqrestore(&c->context_list_lock, flags);
213
214         if (!ptr)
215                 printk(KERN_WARNING "i2o: context id %d not found\n", context);
216
217         pr_debug("get ptr from context list %d -> %p\n", context, ptr);
218
219         return ptr;
220 };
221
222 /**
223  *      i2o_cntxt_list_get_ptr - Get a context id from the context list
224  *      @c: controller to which the context list belong
225  *      @ptr: pointer to which the context id should be fetched
226  *
227  *      Returns context id which matches to the pointer on succes or 0 on
228  *      failure.
229  */
230 u32 i2o_cntxt_list_get_ptr(struct i2o_controller * c, void *ptr)
231 {
232         struct i2o_context_list_element *entry;
233         u32 context = 0;
234         unsigned long flags;
235
236         spin_lock_irqsave(&c->context_list_lock, flags);
237         list_for_each_entry(entry, &c->context_list, list)
238             if (entry->ptr == ptr) {
239                 context = entry->context;
240                 break;
241         }
242         spin_unlock_irqrestore(&c->context_list_lock, flags);
243
244         if (!context)
245                 printk(KERN_WARNING "i2o: Could not find nonexistent ptr "
246                        "%p\n", ptr);
247
248         pr_debug("get context id from context list %p -> %d\n", ptr, context);
249
250         return context;
251 };
252 #endif
253
254 /**
255  *      i2o_iop_find - Find an I2O controller by id
256  *      @unit: unit number of the I2O controller to search for
257  *
258  *      Lookup the I2O controller on the controller list.
259  *
260  *      Returns pointer to the I2O controller on success or NULL if not found.
261  */
262 struct i2o_controller *i2o_find_iop(int unit)
263 {
264         struct i2o_controller *c;
265
266         list_for_each_entry(c, &i2o_controllers, list) {
267                 if (c->unit == unit)
268                         return c;
269         }
270
271         return NULL;
272 };
273
274 /**
275  *      i2o_iop_find_device - Find a I2O device on an I2O controller
276  *      @c: I2O controller where the I2O device hangs on
277  *      @tid: TID of the I2O device to search for
278  *
279  *      Searches the devices of the I2O controller for a device with TID tid and
280  *      returns it.
281  *
282  *      Returns a pointer to the I2O device if found, otherwise NULL.
283  */
284 struct i2o_device *i2o_iop_find_device(struct i2o_controller *c, u16 tid)
285 {
286         struct i2o_device *dev;
287
288         list_for_each_entry(dev, &c->devices, list)
289             if (dev->lct_data.tid == tid)
290                 return dev;
291
292         return NULL;
293 };
294
295 /**
296  *      i2o_quiesce_controller - quiesce controller
297  *      @c: controller
298  *
299  *      Quiesce an IOP. Causes IOP to make external operation quiescent
300  *      (i2o 'READY' state). Internal operation of the IOP continues normally.
301  *
302  *      Returns 0 on success or negative error code on failure.
303  */
304 static int i2o_iop_quiesce(struct i2o_controller *c)
305 {
306         struct i2o_message *msg;
307         u32 m;
308         i2o_status_block *sb = c->status_block.virt;
309         int rc;
310
311         i2o_status_get(c);
312
313         /* SysQuiesce discarded if IOP not in READY or OPERATIONAL state */
314         if ((sb->iop_state != ADAPTER_STATE_READY) &&
315             (sb->iop_state != ADAPTER_STATE_OPERATIONAL))
316                 return 0;
317
318         m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
319         if (m == I2O_QUEUE_EMPTY)
320                 return -ETIMEDOUT;
321
322         writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
323         writel(I2O_CMD_SYS_QUIESCE << 24 | HOST_TID << 12 | ADAPTER_TID,
324                &msg->u.head[1]);
325
326         /* Long timeout needed for quiesce if lots of devices */
327         if ((rc = i2o_msg_post_wait(c, m, 240)))
328                 printk(KERN_INFO "%s: Unable to quiesce (status=%#x).\n",
329                        c->name, -rc);
330         else
331                 pr_debug("%s: Quiesced.\n", c->name);
332
333         i2o_status_get(c);      // Entered READY state
334
335         return rc;
336 };
337
338 /**
339  *      i2o_iop_enable - move controller from ready to OPERATIONAL
340  *      @c: I2O controller
341  *
342  *      Enable IOP. This allows the IOP to resume external operations and
343  *      reverses the effect of a quiesce. Returns zero or an error code if
344  *      an error occurs.
345  */
346 static int i2o_iop_enable(struct i2o_controller *c)
347 {
348         struct i2o_message *msg;
349         u32 m;
350         i2o_status_block *sb = c->status_block.virt;
351         int rc;
352
353         i2o_status_get(c);
354
355         /* Enable only allowed on READY state */
356         if (sb->iop_state != ADAPTER_STATE_READY)
357                 return -EINVAL;
358
359         m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
360         if (m == I2O_QUEUE_EMPTY)
361                 return -ETIMEDOUT;
362
363         writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
364         writel(I2O_CMD_SYS_ENABLE << 24 | HOST_TID << 12 | ADAPTER_TID,
365                &msg->u.head[1]);
366
367         /* How long of a timeout do we need? */
368         if ((rc = i2o_msg_post_wait(c, m, 240)))
369                 printk(KERN_ERR "%s: Could not enable (status=%#x).\n",
370                        c->name, -rc);
371         else
372                 pr_debug("%s: Enabled.\n", c->name);
373
374         i2o_status_get(c);      // entered OPERATIONAL state
375
376         return rc;
377 };
378
379 /**
380  *      i2o_iop_quiesce_all - Quiesce all I2O controllers on the system
381  *
382  *      Quiesce all I2O controllers which are connected to the system.
383  */
384 static inline void i2o_iop_quiesce_all(void)
385 {
386         struct i2o_controller *c, *tmp;
387
388         list_for_each_entry_safe(c, tmp, &i2o_controllers, list) {
389                 if (!c->no_quiesce)
390                         i2o_iop_quiesce(c);
391         }
392 };
393
394 /**
395  *      i2o_iop_enable_all - Enables all controllers on the system
396  *
397  *      Enables all I2O controllers which are connected to the system.
398  */
399 static inline void i2o_iop_enable_all(void)
400 {
401         struct i2o_controller *c, *tmp;
402
403         list_for_each_entry_safe(c, tmp, &i2o_controllers, list)
404             i2o_iop_enable(c);
405 };
406
407 /**
408  *      i2o_clear_controller - Bring I2O controller into HOLD state
409  *      @c: controller
410  *
411  *      Clear an IOP to HOLD state, ie. terminate external operations, clear all
412  *      input queues and prepare for a system restart. IOP's internal operation
413  *      continues normally and the outbound queue is alive. The IOP is not
414  *      expected to rebuild its LCT.
415  *
416  *      Returns 0 on success or negative error code on failure.
417  */
418 static int i2o_iop_clear(struct i2o_controller *c)
419 {
420         struct i2o_message *msg;
421         u32 m;
422         int rc;
423
424         m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
425         if (m == I2O_QUEUE_EMPTY)
426                 return -ETIMEDOUT;
427
428         /* Quiesce all IOPs first */
429         i2o_iop_quiesce_all();
430
431         writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
432         writel(I2O_CMD_ADAPTER_CLEAR << 24 | HOST_TID << 12 | ADAPTER_TID,
433                &msg->u.head[1]);
434
435         if ((rc = i2o_msg_post_wait(c, m, 30)))
436                 printk(KERN_INFO "%s: Unable to clear (status=%#x).\n",
437                        c->name, -rc);
438         else
439                 pr_debug("%s: Cleared.\n", c->name);
440
441         /* Enable all IOPs */
442         i2o_iop_enable_all();
443
444         i2o_status_get(c);
445
446         return rc;
447 }
448
449 /**
450  *      i2o_iop_reset - reset an I2O controller
451  *      @c: controller to reset
452  *
453  *      Reset the IOP into INIT state and wait until IOP gets into RESET state.
454  *      Terminate all external operations, clear IOP's inbound and outbound
455  *      queues, terminate all DDMs, and reload the IOP's operating environment
456  *      and all local DDMs. The IOP rebuilds its LCT.
457  */
458 static int i2o_iop_reset(struct i2o_controller *c)
459 {
460         u8 *status = c->status.virt;
461         struct i2o_message *msg;
462         u32 m;
463         unsigned long timeout;
464         i2o_status_block *sb = c->status_block.virt;
465         int rc = 0;
466
467         pr_debug("Resetting controller\n");
468
469         m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
470         if (m == I2O_QUEUE_EMPTY)
471                 return -ETIMEDOUT;
472
473         memset(status, 0, 4);
474
475         /* Quiesce all IOPs first */
476         i2o_iop_quiesce_all();
477
478         writel(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
479         writel(I2O_CMD_ADAPTER_RESET << 24 | HOST_TID << 12 | ADAPTER_TID,
480                &msg->u.head[1]);
481         writel(i2o_exec_driver.context, &msg->u.s.icntxt);
482         writel(0, &msg->u.s.tcntxt);    //FIXME: use reasonable transaction context
483         writel(0, &msg->body[0]);
484         writel(0, &msg->body[1]);
485         writel(i2o_ptr_low((void *)c->status.phys), &msg->body[2]);
486         writel(i2o_ptr_high((void *)c->status.phys), &msg->body[3]);
487
488         i2o_msg_post(c, m);
489
490         /* Wait for a reply */
491         timeout = jiffies + I2O_TIMEOUT_RESET * HZ;
492         while (!*status) {
493                 if (time_after(jiffies, timeout)) {
494                         printk(KERN_ERR "IOP reset timeout.\n");
495                         rc = -ETIMEDOUT;
496                         goto exit;
497                 }
498                 set_current_state(TASK_UNINTERRUPTIBLE);
499                 schedule_timeout(1);
500
501                 rmb();
502         }
503
504         if (*status == I2O_CMD_IN_PROGRESS) {
505                 /*
506                  * Once the reset is sent, the IOP goes into the INIT state
507                  * which is indeterminate.  We need to wait until the IOP
508                  * has rebooted before we can let the system talk to
509                  * it. We read the inbound Free_List until a message is
510                  * available. If we can't read one in the given ammount of
511                  * time, we assume the IOP could not reboot properly.
512                  */
513                 pr_debug("%s: Reset in progress, waiting for reboot...\n",
514                          c->name);
515
516                 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_RESET);
517                 while (m == I2O_QUEUE_EMPTY) {
518                         if (time_after(jiffies, timeout)) {
519                                 printk(KERN_ERR "IOP reset timeout.\n");
520                                 rc = -ETIMEDOUT;
521                                 goto exit;
522                         }
523                         set_current_state(TASK_UNINTERRUPTIBLE);
524                         schedule_timeout(1);
525
526                         m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_RESET);
527                 }
528                 i2o_msg_nop(c, m);
529         }
530
531         /* from here all quiesce commands are safe */
532         c->no_quiesce = 0;
533
534         /* If IopReset was rejected or didn't perform reset, try IopClear */
535         i2o_status_get(c);
536         if (*status == I2O_CMD_REJECTED || sb->iop_state != ADAPTER_STATE_RESET) {
537                 printk(KERN_WARNING "%s: Reset rejected, trying to clear\n",
538                        c->name);
539                 i2o_iop_clear(c);
540         } else
541                 pr_debug("%s: Reset completed.\n", c->name);
542
543       exit:
544         /* Enable all IOPs */
545         i2o_iop_enable_all();
546
547         return rc;
548 };
549
550 /**
551  *      i2o_iop_init_outbound_queue - setup the outbound message queue
552  *      @c: I2O controller
553  *
554  *      Clear and (re)initialize IOP's outbound queue and post the message
555  *      frames to the IOP.
556  *
557  *      Returns 0 on success or a negative errno code on failure.
558  */
559 int i2o_iop_init_outbound_queue(struct i2o_controller *c)
560 {
561         u8 *status = c->status.virt;
562         u32 m;
563         struct i2o_message *msg;
564         ulong timeout;
565         int i;
566
567         pr_debug("%s: Initializing Outbound Queue...\n", c->name);
568
569         memset(status, 0, 4);
570
571         m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
572         if (m == I2O_QUEUE_EMPTY)
573                 return -ETIMEDOUT;
574
575         writel(EIGHT_WORD_MSG_SIZE | TRL_OFFSET_6, &msg->u.head[0]);
576         writel(I2O_CMD_OUTBOUND_INIT << 24 | HOST_TID << 12 | ADAPTER_TID,
577                &msg->u.head[1]);
578         writel(i2o_exec_driver.context, &msg->u.s.icntxt);
579         writel(0x0106, &msg->u.s.tcntxt);       /* FIXME: why 0x0106, maybe in
580                                                    Spec? */
581         writel(PAGE_SIZE, &msg->body[0]);
582         writel(MSG_FRAME_SIZE << 16 | 0x80, &msg->body[1]);     /* Outbound msg frame
583                                                                    size in words and Initcode */
584         writel(0xd0000004, &msg->body[2]);
585         writel(i2o_ptr_low((void *)c->status.phys), &msg->body[3]);
586         writel(i2o_ptr_high((void *)c->status.phys), &msg->body[4]);
587
588         i2o_msg_post(c, m);
589
590         timeout = jiffies + I2O_TIMEOUT_INIT_OUTBOUND_QUEUE * HZ;
591         while (*status <= I2O_CMD_IN_PROGRESS) {
592                 if (time_after(jiffies, timeout)) {
593                         printk(KERN_WARNING "%s: Timeout Initializing\n",
594                                c->name);
595                         return -ETIMEDOUT;
596                 }
597                 set_current_state(TASK_UNINTERRUPTIBLE);
598                 schedule_timeout(1);
599
600                 rmb();
601         }
602
603         m = c->out_queue.phys;
604
605         /* Post frames */
606         for (i = 0; i < NMBR_MSG_FRAMES; i++) {
607                 i2o_flush_reply(c, m);
608                 m += MSG_FRAME_SIZE * 4;
609         }
610
611         return 0;
612 }
613
614 /**
615  *      i2o_iop_activate - Bring controller up to HOLD
616  *      @c: controller
617  *
618  *      This function brings an I2O controller into HOLD state. The adapter
619  *      is reset if necessary and then the queues and resource table are read.
620  *
621  *      Returns 0 on success or negative error code on failure.
622  */
623 static int i2o_iop_activate(struct i2o_controller *c)
624 {
625         i2o_status_block *sb = c->status_block.virt;
626         int rc;
627         /* In INIT state, Wait Inbound Q to initialize (in i2o_status_get) */
628         /* In READY state, Get status */
629
630         rc = i2o_status_get(c);
631         if (rc) {
632                 printk(KERN_INFO "Unable to obtain status of %s, "
633                        "attempting a reset.\n", c->name);
634                 if (i2o_iop_reset(c))
635                         return rc;
636         }
637
638         if (sb->i2o_version > I2OVER15) {
639                 printk(KERN_ERR "%s: Not running vrs. 1.5. of the I2O "
640                        "Specification.\n", c->name);
641                 return -ENODEV;
642         }
643
644         switch (sb->iop_state) {
645         case ADAPTER_STATE_FAULTED:
646                 printk(KERN_CRIT "%s: hardware fault\n", c->name);
647                 return -ENODEV;
648
649         case ADAPTER_STATE_READY:
650         case ADAPTER_STATE_OPERATIONAL:
651         case ADAPTER_STATE_HOLD:
652         case ADAPTER_STATE_FAILED:
653                 pr_debug("already running, trying to reset...\n");
654                 if (i2o_iop_reset(c))
655                         return -ENODEV;
656         }
657
658         rc = i2o_iop_init_outbound_queue(c);
659         if (rc)
660                 return rc;
661
662         /* In HOLD state */
663
664         rc = i2o_hrt_get(c);
665         if (rc)
666                 return rc;
667
668         return 0;
669 };
670
671 /**
672  *      i2o_iop_systab_set - Set the I2O System Table of the specified IOP
673  *      @c: I2O controller to which the system table should be send
674  *
675  *      Before the systab could be set i2o_systab_build() must be called.
676  *
677  *      Returns 0 on success or negative error code on failure.
678  */
679 static int i2o_iop_systab_set(struct i2o_controller *c)
680 {
681         struct i2o_message *msg;
682         u32 m;
683         i2o_status_block *sb = c->status_block.virt;
684         struct device *dev = &c->pdev->dev;
685         struct resource *root;
686         int rc;
687
688         if (sb->current_mem_size < sb->desired_mem_size) {
689                 struct resource *res = &c->mem_resource;
690                 res->name = c->pdev->bus->name;
691                 res->flags = IORESOURCE_MEM;
692                 res->start = 0;
693                 res->end = 0;
694                 printk("%s: requires private memory resources.\n", c->name);
695                 root = pci_find_parent_resource(c->pdev, res);
696                 if (root == NULL)
697                         printk("Can't find parent resource!\n");
698                 if (root && allocate_resource(root, res, sb->desired_mem_size, sb->desired_mem_size, sb->desired_mem_size, 1 << 20,     /* Unspecified, so use 1Mb and play safe */
699                                               NULL, NULL) >= 0) {
700                         c->mem_alloc = 1;
701                         sb->current_mem_size = 1 + res->end - res->start;
702                         sb->current_mem_base = res->start;
703                         printk(KERN_INFO
704                                "%s: allocated %ld bytes of PCI memory at 0x%08lX.\n",
705                                c->name, 1 + res->end - res->start, res->start);
706                 }
707         }
708
709         if (sb->current_io_size < sb->desired_io_size) {
710                 struct resource *res = &c->io_resource;
711                 res->name = c->pdev->bus->name;
712                 res->flags = IORESOURCE_IO;
713                 res->start = 0;
714                 res->end = 0;
715                 printk("%s: requires private memory resources.\n", c->name);
716                 root = pci_find_parent_resource(c->pdev, res);
717                 if (root == NULL)
718                         printk("Can't find parent resource!\n");
719                 if (root && allocate_resource(root, res, sb->desired_io_size, sb->desired_io_size, sb->desired_io_size, 1 << 20,        /* Unspecified, so use 1Mb and play safe */
720                                               NULL, NULL) >= 0) {
721                         c->io_alloc = 1;
722                         sb->current_io_size = 1 + res->end - res->start;
723                         sb->current_mem_base = res->start;
724                         printk(KERN_INFO
725                                "%s: allocated %ld bytes of PCI I/O at 0x%08lX.\n",
726                                c->name, 1 + res->end - res->start, res->start);
727                 }
728         }
729
730         m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
731         if (m == I2O_QUEUE_EMPTY)
732                 return -ETIMEDOUT;
733
734         i2o_systab.phys = dma_map_single(dev, i2o_systab.virt, i2o_systab.len,
735                                          PCI_DMA_TODEVICE);
736         if (!i2o_systab.phys) {
737                 i2o_msg_nop(c, m);
738                 return -ENOMEM;
739         }
740
741         writel(I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6, &msg->u.head[0]);
742         writel(I2O_CMD_SYS_TAB_SET << 24 | HOST_TID << 12 | ADAPTER_TID,
743                &msg->u.head[1]);
744
745         /*
746          * Provide three SGL-elements:
747          * System table (SysTab), Private memory space declaration and
748          * Private i/o space declaration
749          *
750          * FIXME: is this still true?
751          * Nasty one here. We can't use dma_alloc_coherent to send the
752          * same table to everyone. We have to go remap it for them all
753          */
754
755         writel(c->unit + 2, &msg->body[0]);
756         writel(0, &msg->body[1]);
757         writel(0x54000000 | i2o_systab.phys, &msg->body[2]);
758         writel(i2o_systab.phys, &msg->body[3]);
759         writel(0x54000000 | sb->current_mem_size, &msg->body[4]);
760         writel(sb->current_mem_base, &msg->body[5]);
761         writel(0xd4000000 | sb->current_io_size, &msg->body[6]);
762         writel(sb->current_io_base, &msg->body[6]);
763
764         rc = i2o_msg_post_wait(c, m, 120);
765
766         dma_unmap_single(dev, i2o_systab.phys, i2o_systab.len,
767                          PCI_DMA_TODEVICE);
768
769         if (rc < 0)
770                 printk(KERN_ERR "%s: Unable to set SysTab (status=%#x).\n",
771                        c->name, -rc);
772         else
773                 pr_debug("%s: SysTab set.\n", c->name);
774
775         i2o_status_get(c);      // Entered READY state
776
777         return rc;
778 }
779
780 /**
781  *      i2o_iop_online - Bring a controller online into OPERATIONAL state.
782  *      @c: I2O controller
783  *
784  *      Send the system table and enable the I2O controller.
785  *
786  *      Returns 0 on success or negativer error code on failure.
787  */
788 static int i2o_iop_online(struct i2o_controller *c)
789 {
790         int rc;
791
792         rc = i2o_iop_systab_set(c);
793         if (rc)
794                 return rc;
795
796         /* In READY state */
797         pr_debug("%s: Attempting to enable...\n", c->name);
798         rc = i2o_iop_enable(c);
799         if (rc)
800                 return rc;
801
802         return 0;
803 };
804
805 /**
806  *      i2o_iop_remove - Remove the I2O controller from the I2O core
807  *      @c: I2O controller
808  *
809  *      Remove the I2O controller from the I2O core. If devices are attached to
810  *      the controller remove these also and finally reset the controller.
811  */
812 void i2o_iop_remove(struct i2o_controller *c)
813 {
814         struct i2o_device *dev, *tmp;
815
816         pr_debug("Deleting controller %s\n", c->name);
817
818         i2o_driver_notify_controller_remove_all(c);
819
820         list_del(&c->list);
821
822         list_for_each_entry_safe(dev, tmp, &c->devices, list)
823             i2o_device_remove(dev);
824
825         /* Ask the IOP to switch to RESET state */
826         i2o_iop_reset(c);
827 }
828
829 /**
830  *      i2o_systab_build - Build system table
831  *
832  *      The system table contains information about all the IOPs in the system
833  *      (duh) and is used by the Executives on the IOPs to establish peer2peer
834  *      connections. We're not supporting peer2peer at the moment, but this
835  *      will be needed down the road for things like lan2lan forwarding.
836  *
837  *      Returns 0 on success or negative error code on failure.
838  */
839 static int i2o_systab_build(void)
840 {
841         struct i2o_controller *c, *tmp;
842         int num_controllers = 0;
843         u32 change_ind = 0;
844         int count = 0;
845         struct i2o_sys_tbl *systab = i2o_systab.virt;
846
847         list_for_each_entry_safe(c, tmp, &i2o_controllers, list)
848             num_controllers++;
849
850         if (systab) {
851                 change_ind = systab->change_ind;
852                 kfree(i2o_systab.virt);
853         }
854
855         /* Header + IOPs */
856         i2o_systab.len = sizeof(struct i2o_sys_tbl) + num_controllers *
857             sizeof(struct i2o_sys_tbl_entry);
858
859         systab = i2o_systab.virt = kmalloc(i2o_systab.len, GFP_KERNEL);
860         if (!systab) {
861                 printk(KERN_ERR "i2o: unable to allocate memory for System "
862                        "Table\n");
863                 return -ENOMEM;
864         }
865         memset(systab, 0, i2o_systab.len);
866
867         systab->version = I2OVERSION;
868         systab->change_ind = change_ind + 1;
869
870         list_for_each_entry_safe(c, tmp, &i2o_controllers, list) {
871                 i2o_status_block *sb;
872
873                 if (count >= num_controllers) {
874                         printk(KERN_ERR "i2o: controller added while building "
875                                "system table\n");
876                         break;
877                 }
878
879                 sb = c->status_block.virt;
880
881                 /*
882                  * Get updated IOP state so we have the latest information
883                  *
884                  * We should delete the controller at this point if it
885                  * doesn't respond since if it's not on the system table
886                  * it is techninically not part of the I2O subsystem...
887                  */
888                 if (unlikely(i2o_status_get(c))) {
889                         printk(KERN_ERR "%s: Deleting b/c could not get status"
890                                " while attempting to build system table\n",
891                                c->name);
892                         i2o_iop_remove(c);
893                         continue;       // try the next one
894                 }
895
896                 systab->iops[count].org_id = sb->org_id;
897                 systab->iops[count].iop_id = c->unit + 2;
898                 systab->iops[count].seg_num = 0;
899                 systab->iops[count].i2o_version = sb->i2o_version;
900                 systab->iops[count].iop_state = sb->iop_state;
901                 systab->iops[count].msg_type = sb->msg_type;
902                 systab->iops[count].frame_size = sb->inbound_frame_size;
903                 systab->iops[count].last_changed = change_ind;
904                 systab->iops[count].iop_capabilities = sb->iop_capabilities;
905                 systab->iops[count].inbound_low = i2o_ptr_low(c->post_port);
906                 systab->iops[count].inbound_high = i2o_ptr_high(c->post_port);
907
908                 count++;
909         }
910
911         systab->num_entries = count;
912
913         return 0;
914 };
915
916 /**
917  *      i2o_parse_hrt - Parse the hardware resource table.
918  *      @c: I2O controller
919  *
920  *      We don't do anything with it except dumping it (in debug mode).
921  *
922  *      Returns 0.
923  */
924 static int i2o_parse_hrt(struct i2o_controller *c)
925 {
926         i2o_dump_hrt(c);
927         return 0;
928 };
929
930 /**
931  *      i2o_status_get - Get the status block from the I2O controller
932  *      @c: I2O controller
933  *
934  *      Issue a status query on the controller. This updates the attached
935  *      status block. The status block could then be accessed through
936  *      c->status_block.
937  *
938  *      Returns 0 on sucess or negative error code on failure.
939  */
940 int i2o_status_get(struct i2o_controller *c)
941 {
942         struct i2o_message *msg;
943         u32 m;
944         u8 *status_block;
945         unsigned long timeout;
946
947         status_block = (u8 *) c->status_block.virt;
948         memset(status_block, 0, sizeof(i2o_status_block));
949
950         m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
951         if (m == I2O_QUEUE_EMPTY)
952                 return -ETIMEDOUT;
953
954         writel(NINE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
955         writel(I2O_CMD_STATUS_GET << 24 | HOST_TID << 12 | ADAPTER_TID,
956                &msg->u.head[1]);
957         writel(i2o_exec_driver.context, &msg->u.s.icntxt);
958         writel(0, &msg->u.s.tcntxt);    // FIXME: use resonable transaction context
959         writel(0, &msg->body[0]);
960         writel(0, &msg->body[1]);
961         writel(i2o_ptr_low((void *)c->status_block.phys), &msg->body[2]);
962         writel(i2o_ptr_high((void *)c->status_block.phys), &msg->body[3]);
963         writel(sizeof(i2o_status_block), &msg->body[4]);        /* always 88 bytes */
964
965         i2o_msg_post(c, m);
966
967         /* Wait for a reply */
968         timeout = jiffies + I2O_TIMEOUT_STATUS_GET * HZ;
969         while (status_block[87] != 0xFF) {
970                 if (time_after(jiffies, timeout)) {
971                         printk(KERN_ERR "%s: Get status timeout.\n", c->name);
972                         return -ETIMEDOUT;
973                 }
974
975                 set_current_state(TASK_UNINTERRUPTIBLE);
976                 schedule_timeout(1);
977
978                 rmb();
979         }
980
981 #ifdef DEBUG
982         i2o_debug_state(c);
983 #endif
984
985         return 0;
986 }
987
988 /*
989  *      i2o_hrt_get - Get the Hardware Resource Table from the I2O controller
990  *      @c: I2O controller from which the HRT should be fetched
991  *
992  *      The HRT contains information about possible hidden devices but is
993  *      mostly useless to us.
994  *
995  *      Returns 0 on success or negativer error code on failure.
996  */
997 int i2o_hrt_get(struct i2o_controller *c)
998 {
999         int rc;
1000         int i;
1001         i2o_hrt *hrt = c->hrt.virt;
1002         u32 size = sizeof(i2o_hrt);
1003         struct device *dev = &c->pdev->dev;
1004
1005         for (i = 0; i < I2O_HRT_GET_TRIES; i++) {
1006                 struct i2o_message *msg;
1007                 u32 m;
1008
1009                 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
1010                 if (m == I2O_QUEUE_EMPTY)
1011                         return -ETIMEDOUT;
1012
1013                 writel(SIX_WORD_MSG_SIZE | SGL_OFFSET_4, &msg->u.head[0]);
1014                 writel(I2O_CMD_HRT_GET << 24 | HOST_TID << 12 | ADAPTER_TID,
1015                        &msg->u.head[1]);
1016                 writel(0xd0000000 | c->hrt.len, &msg->body[0]);
1017                 writel(c->hrt.phys, &msg->body[1]);
1018
1019                 rc = i2o_msg_post_wait_mem(c, m, 20, &c->hrt);
1020
1021                 if (rc < 0) {
1022                         printk(KERN_ERR "%s: Unable to get HRT (status=%#x)\n",
1023                                c->name, -rc);
1024                         return rc;
1025                 }
1026
1027                 size = hrt->num_entries * hrt->entry_len << 2;
1028                 if (size > c->hrt.len) {
1029                         if (i2o_dma_realloc(dev, &c->hrt, size, GFP_KERNEL))
1030                                 return -ENOMEM;
1031                         else
1032                                 hrt = c->hrt.virt;
1033                 } else
1034                         return i2o_parse_hrt(c);
1035         }
1036
1037         printk(KERN_ERR "%s: Unable to get HRT after %d tries, giving up\n",
1038                c->name, I2O_HRT_GET_TRIES);
1039
1040         return -EBUSY;
1041 }
1042
1043 /**
1044  *      i2o_iop_alloc - Allocate and initialize a i2o_controller struct
1045  *
1046  *      Allocate the necessary memory for a i2o_controller struct and
1047  *      initialize the lists.
1048  *
1049  *      Returns a pointer to the I2O controller or a negative error code on
1050  *      failure.
1051  */
1052 struct i2o_controller *i2o_iop_alloc(void)
1053 {
1054         static int unit = 0;    /* 0 and 1 are NULL IOP and Local Host */
1055         struct i2o_controller *c;
1056
1057         c = kmalloc(sizeof(*c), GFP_KERNEL);
1058         if (!c) {
1059                 printk(KERN_ERR "i2o: Insufficient memory to allocate the "
1060                        "controller.\n");
1061                 return ERR_PTR(-ENOMEM);
1062         }
1063         memset(c, 0, sizeof(*c));
1064
1065         INIT_LIST_HEAD(&c->devices);
1066         c->lock = SPIN_LOCK_UNLOCKED;
1067         init_MUTEX(&c->lct_lock);
1068         c->unit = unit++;
1069         sprintf(c->name, "iop%d", c->unit);
1070
1071 #if BITS_PER_LONG == 64
1072         c->context_list_lock = SPIN_LOCK_UNLOCKED;
1073         atomic_set(&c->context_list_counter, 0);
1074         INIT_LIST_HEAD(&c->context_list);
1075 #endif
1076
1077         return c;
1078 };
1079
1080 /**
1081  *      i2o_iop_free - Free the i2o_controller struct
1082  *      @c: I2O controller to free
1083  */
1084 void i2o_iop_free(struct i2o_controller *c)
1085 {
1086         kfree(c);
1087 };
1088
1089 /**
1090  *      i2o_iop_add - Initialize the I2O controller and add him to the I2O core
1091  *      @c: controller
1092  *
1093  *      Initialize the I2O controller and if no error occurs add him to the I2O
1094  *      core.
1095  *
1096  *      Returns 0 on success or negative error code on failure.
1097  */
1098 int i2o_iop_add(struct i2o_controller *c)
1099 {
1100         int rc;
1101
1102         printk(KERN_INFO "%s: Activating I2O controller...\n", c->name);
1103         printk(KERN_INFO "%s: This may take a few minutes if there are many "
1104                "devices\n", c->name);
1105
1106         if ((rc = i2o_iop_activate(c))) {
1107                 printk(KERN_ERR "%s: controller could not activated\n",
1108                        c->name);
1109                 i2o_iop_reset(c);
1110                 return rc;
1111         }
1112
1113         pr_debug("building sys table %s...\n", c->name);
1114
1115         if ((rc = i2o_systab_build())) {
1116                 i2o_iop_reset(c);
1117                 return rc;
1118         }
1119
1120         pr_debug("online controller %s...\n", c->name);
1121
1122         if ((rc = i2o_iop_online(c))) {
1123                 i2o_iop_reset(c);
1124                 return rc;
1125         }
1126
1127         pr_debug("getting LCT %s...\n", c->name);
1128
1129         if ((rc = i2o_exec_lct_get(c))) {
1130                 i2o_iop_reset(c);
1131                 return rc;
1132         }
1133
1134         list_add(&c->list, &i2o_controllers);
1135
1136         i2o_driver_notify_controller_add_all(c);
1137
1138         printk(KERN_INFO "%s: Controller added\n", c->name);
1139
1140         return 0;
1141 };
1142
1143 /**
1144  *      i2o_event_register - Turn on/off event notification for a I2O device
1145  *      @dev: I2O device which should receive the event registration request
1146  *      @drv: driver which want to get notified
1147  *      @tcntxt: transaction context to use with this notifier
1148  *      @evt_mask: mask of events
1149  *
1150  *      Create and posts an event registration message to the task. No reply
1151  *      is waited for, or expected. If you do not want further notifications,
1152  *      call the i2o_event_register again with a evt_mask of 0.
1153  *
1154  *      Returns 0 on success or -ETIMEDOUT if no message could be fetched for
1155  *      sending the request.
1156  */
1157 int i2o_event_register(struct i2o_device *dev, struct i2o_driver *drv,
1158                        int tcntxt, u32 evt_mask)
1159 {
1160         struct i2o_controller *c = dev->iop;
1161         struct i2o_message *msg;
1162         u32 m;
1163
1164         m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
1165         if (m == I2O_QUEUE_EMPTY)
1166                 return -ETIMEDOUT;
1167
1168         writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
1169         writel(I2O_CMD_UTIL_EVT_REGISTER << 24 | HOST_TID << 12 | dev->lct_data.
1170                tid, &msg->u.head[1]);
1171         writel(drv->context, &msg->u.s.icntxt);
1172         writel(tcntxt, &msg->u.s.tcntxt);
1173         writel(evt_mask, &msg->body[0]);
1174
1175         i2o_msg_post(c, m);
1176
1177         return 0;
1178 };
1179
1180 /**
1181  *      i2o_iop_init - I2O main initialization function
1182  *
1183  *      Initialize the I2O drivers (OSM) functions, register the Executive OSM,
1184  *      initialize the I2O PCI part and finally initialize I2O device stuff.
1185  *
1186  *      Returns 0 on success or negative error code on failure.
1187  */
1188 static int __init i2o_iop_init(void)
1189 {
1190         int rc = 0;
1191
1192         printk(KERN_INFO "I2O Core - (C) Copyright 1999 Red Hat Software\n");
1193
1194         rc = i2o_device_init();
1195         if (rc)
1196                 goto exit;
1197
1198         rc = i2o_driver_init();
1199         if (rc)
1200                 goto device_exit;
1201
1202         rc = i2o_exec_init();
1203         if (rc)
1204                 goto driver_exit;
1205
1206         rc = i2o_pci_init();
1207         if (rc < 0)
1208                 goto exec_exit;
1209
1210         return 0;
1211
1212       exec_exit:
1213         i2o_exec_exit();
1214
1215       driver_exit:
1216         i2o_driver_exit();
1217
1218       device_exit:
1219         i2o_device_exit();
1220
1221       exit:
1222         return rc;
1223 }
1224
1225 /**
1226  *      i2o_iop_exit - I2O main exit function
1227  *
1228  *      Removes I2O controllers from PCI subsystem and shut down OSMs.
1229  */
1230 static void __exit i2o_iop_exit(void)
1231 {
1232         i2o_pci_exit();
1233         i2o_exec_exit();
1234         i2o_driver_exit();
1235         i2o_device_exit();
1236 };
1237
1238 module_init(i2o_iop_init);
1239 module_exit(i2o_iop_exit);
1240
1241 MODULE_AUTHOR("Red Hat Software");
1242 MODULE_DESCRIPTION("I2O Core");
1243 MODULE_LICENSE("GPL");
1244
1245 #if BITS_PER_LONG == 64
1246 EXPORT_SYMBOL(i2o_cntxt_list_add);
1247 EXPORT_SYMBOL(i2o_cntxt_list_get);
1248 EXPORT_SYMBOL(i2o_cntxt_list_remove);
1249 EXPORT_SYMBOL(i2o_cntxt_list_get_ptr);
1250 #endif
1251 EXPORT_SYMBOL(i2o_msg_get_wait);
1252 EXPORT_SYMBOL(i2o_msg_nop);
1253 EXPORT_SYMBOL(i2o_find_iop);
1254 EXPORT_SYMBOL(i2o_iop_find_device);
1255 EXPORT_SYMBOL(i2o_event_register);
1256 EXPORT_SYMBOL(i2o_status_get);
1257 EXPORT_SYMBOL(i2o_hrt_get);
1258 EXPORT_SYMBOL(i2o_controllers);