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