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