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