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