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