linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / drivers / message / i2o / exec-osm.c
1 /*
2  *      Executive OSM
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 Red
14  *      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  *              Markus Lidel <Markus.Lidel@shadowconnect.com>:
27  *                      Support for sysfs included.
28  */
29
30 #include <linux/module.h>
31 #include <linux/i2o.h>
32 #include <linux/delay.h>
33 #include <linux/workqueue.h>
34 #include <linux/string.h>
35 #include <linux/slab.h>
36 #include <linux/sched.h>        /* wait_event_interruptible_timeout() needs this */
37 #include <asm/param.h>          /* HZ */
38 #include "core.h"
39
40 #define OSM_NAME "exec-osm"
41
42 struct i2o_driver i2o_exec_driver;
43
44 static int i2o_exec_lct_notify(struct i2o_controller *c, u32 change_ind);
45
46 /* global wait list for POST WAIT */
47 static LIST_HEAD(i2o_exec_wait_list);
48
49 /* Wait struct needed for POST WAIT */
50 struct i2o_exec_wait {
51         wait_queue_head_t *wq;  /* Pointer to Wait queue */
52         struct i2o_dma dma;     /* DMA buffers to free on failure */
53         u32 tcntxt;             /* transaction context from reply */
54         int complete;           /* 1 if reply received otherwise 0 */
55         u32 m;                  /* message id */
56         struct i2o_message *msg;        /* pointer to the reply message */
57         struct list_head list;  /* node in global wait list */
58         spinlock_t lock;        /* lock before modifying */
59 };
60
61 /* Exec OSM class handling definition */
62 static struct i2o_class_id i2o_exec_class_id[] = {
63         {I2O_CLASS_EXECUTIVE},
64         {I2O_CLASS_END}
65 };
66
67 /**
68  *      i2o_exec_wait_alloc - Allocate a i2o_exec_wait struct an initialize it
69  *
70  *      Allocate the i2o_exec_wait struct and initialize the wait.
71  *
72  *      Returns i2o_exec_wait pointer on success or negative error code on
73  *      failure.
74  */
75 static struct i2o_exec_wait *i2o_exec_wait_alloc(void)
76 {
77         struct i2o_exec_wait *wait;
78
79         wait = kzalloc(sizeof(*wait), GFP_KERNEL);
80         if (!wait)
81                 return NULL;
82
83         INIT_LIST_HEAD(&wait->list);
84         spin_lock_init(&wait->lock);
85
86         return wait;
87 };
88
89 /**
90  *      i2o_exec_wait_free - Free a i2o_exec_wait struct
91  *      @i2o_exec_wait: I2O wait data which should be cleaned up
92  */
93 static void i2o_exec_wait_free(struct i2o_exec_wait *wait)
94 {
95         kfree(wait);
96 };
97
98 /**
99  *      i2o_msg_post_wait_mem - Post and wait a message with DMA buffers
100  *      @c: controller
101  *      @m: message to post
102  *      @timeout: time in seconds to wait
103  *      @dma: i2o_dma struct of the DMA buffer to free on failure
104  *
105  *      This API allows an OSM to post a message and then be told whether or
106  *      not the system received a successful reply. If the message times out
107  *      then the value '-ETIMEDOUT' is returned. This is a special case. In
108  *      this situation the message may (should) complete at an indefinite time
109  *      in the future. When it completes it will use the memory buffer
110  *      attached to the request. If -ETIMEDOUT is returned then the memory
111  *      buffer must not be freed. Instead the event completion will free them
112  *      for you. In all other cases the buffer are your problem.
113  *
114  *      Returns 0 on success, negative error code on timeout or positive error
115  *      code from reply.
116  */
117 int i2o_msg_post_wait_mem(struct i2o_controller *c, struct i2o_message *msg,
118                           unsigned long timeout, struct i2o_dma *dma)
119 {
120         DECLARE_WAIT_QUEUE_HEAD(wq);
121         struct i2o_exec_wait *wait;
122         static u32 tcntxt = 0x80000000;
123         long flags;
124         int rc = 0;
125
126         wait = i2o_exec_wait_alloc();
127         if (!wait)
128                 return -ENOMEM;
129
130         if (tcntxt == 0xffffffff)
131                 tcntxt = 0x80000000;
132
133         if (dma)
134                 wait->dma = *dma;
135
136         /*
137          * Fill in the message initiator context and transaction context.
138          * We will only use transaction contexts >= 0x80000000 for POST WAIT,
139          * so we could find a POST WAIT reply easier in the reply handler.
140          */
141         msg->u.s.icntxt = cpu_to_le32(i2o_exec_driver.context);
142         wait->tcntxt = tcntxt++;
143         msg->u.s.tcntxt = cpu_to_le32(wait->tcntxt);
144
145         wait->wq = &wq;
146         /*
147          * we add elements to the head, because if a entry in the list will
148          * never be removed, we have to iterate over it every time
149          */
150         list_add(&wait->list, &i2o_exec_wait_list);
151
152         /*
153          * Post the message to the controller. At some point later it will
154          * return. If we time out before it returns then complete will be zero.
155          */
156         i2o_msg_post(c, msg);
157
158         wait_event_interruptible_timeout(wq, wait->complete, timeout * HZ);
159
160         spin_lock_irqsave(&wait->lock, flags);
161
162         wait->wq = NULL;
163
164         if (wait->complete)
165                 rc = le32_to_cpu(wait->msg->body[0]) >> 24;
166         else {
167                 /*
168                  * We cannot remove it now. This is important. When it does
169                  * terminate (which it must do if the controller has not
170                  * died...) then it will otherwise scribble on stuff.
171                  *
172                  * FIXME: try abort message
173                  */
174                 if (dma)
175                         dma->virt = NULL;
176
177                 rc = -ETIMEDOUT;
178         }
179
180         spin_unlock_irqrestore(&wait->lock, flags);
181
182         if (rc != -ETIMEDOUT) {
183                 i2o_flush_reply(c, wait->m);
184                 i2o_exec_wait_free(wait);
185         }
186
187         return rc;
188 };
189
190 /**
191  *      i2o_msg_post_wait_complete - Reply to a i2o_msg_post request from IOP
192  *      @c: I2O controller which answers
193  *      @m: message id
194  *      @msg: pointer to the I2O reply message
195  *      @context: transaction context of request
196  *
197  *      This function is called in interrupt context only. If the reply reached
198  *      before the timeout, the i2o_exec_wait struct is filled with the message
199  *      and the task will be waked up. The task is now responsible for returning
200  *      the message m back to the controller! If the message reaches us after
201  *      the timeout clean up the i2o_exec_wait struct (including allocated
202  *      DMA buffer).
203  *
204  *      Return 0 on success and if the message m should not be given back to the
205  *      I2O controller, or >0 on success and if the message should be given back
206  *      afterwords. Returns negative error code on failure. In this case the
207  *      message must also be given back to the controller.
208  */
209 static int i2o_msg_post_wait_complete(struct i2o_controller *c, u32 m,
210                                       struct i2o_message *msg, u32 context)
211 {
212         struct i2o_exec_wait *wait, *tmp;
213         unsigned long flags;
214         int rc = 1;
215
216         /*
217          * We need to search through the i2o_exec_wait_list to see if the given
218          * message is still outstanding. If not, it means that the IOP took
219          * longer to respond to the message than we had allowed and timer has
220          * already expired. Not much we can do about that except log it for
221          * debug purposes, increase timeout, and recompile.
222          */
223         list_for_each_entry_safe(wait, tmp, &i2o_exec_wait_list, list) {
224                 if (wait->tcntxt == context) {
225                         spin_lock_irqsave(&wait->lock, flags);
226
227                         list_del(&wait->list);
228
229                         wait->m = m;
230                         wait->msg = msg;
231                         wait->complete = 1;
232
233                         if (wait->wq)
234                                 rc = 0;
235                         else
236                                 rc = -1;
237
238                         spin_unlock_irqrestore(&wait->lock, flags);
239
240                         if (rc) {
241                                 struct device *dev;
242
243                                 dev = &c->pdev->dev;
244
245                                 pr_debug("%s: timedout reply received!\n",
246                                          c->name);
247                                 i2o_dma_free(dev, &wait->dma);
248                                 i2o_exec_wait_free(wait);
249                         } else
250                                 wake_up_interruptible(wait->wq);
251
252                         return rc;
253                 }
254         }
255
256         osm_warn("%s: Bogus reply in POST WAIT (tr-context: %08x)!\n", c->name,
257                  context);
258
259         return -1;
260 };
261
262 /**
263  *      i2o_exec_show_vendor_id - Displays Vendor ID of controller
264  *      @d: device of which the Vendor ID should be displayed
265  *      @buf: buffer into which the Vendor ID should be printed
266  *
267  *      Returns number of bytes printed into buffer.
268  */
269 static ssize_t i2o_exec_show_vendor_id(struct device *d,
270                                        struct device_attribute *attr, char *buf)
271 {
272         struct i2o_device *dev = to_i2o_device(d);
273         u16 id;
274
275         if (!i2o_parm_field_get(dev, 0x0000, 0, &id, 2)) {
276                 sprintf(buf, "0x%04x", le16_to_cpu(id));
277                 return strlen(buf) + 1;
278         }
279
280         return 0;
281 };
282
283 /**
284  *      i2o_exec_show_product_id - Displays Product ID of controller
285  *      @d: device of which the Product ID should be displayed
286  *      @buf: buffer into which the Product ID should be printed
287  *
288  *      Returns number of bytes printed into buffer.
289  */
290 static ssize_t i2o_exec_show_product_id(struct device *d,
291                                         struct device_attribute *attr,
292                                         char *buf)
293 {
294         struct i2o_device *dev = to_i2o_device(d);
295         u16 id;
296
297         if (!i2o_parm_field_get(dev, 0x0000, 1, &id, 2)) {
298                 sprintf(buf, "0x%04x", le16_to_cpu(id));
299                 return strlen(buf) + 1;
300         }
301
302         return 0;
303 };
304
305 /* Exec-OSM device attributes */
306 static DEVICE_ATTR(vendor_id, S_IRUGO, i2o_exec_show_vendor_id, NULL);
307 static DEVICE_ATTR(product_id, S_IRUGO, i2o_exec_show_product_id, NULL);
308
309 /**
310  *      i2o_exec_probe - Called if a new I2O device (executive class) appears
311  *      @dev: I2O device which should be probed
312  *
313  *      Registers event notification for every event from Executive device. The
314  *      return is always 0, because we want all devices of class Executive.
315  *
316  *      Returns 0 on success.
317  */
318 static int i2o_exec_probe(struct device *dev)
319 {
320         struct i2o_device *i2o_dev = to_i2o_device(dev);
321
322         i2o_event_register(i2o_dev, &i2o_exec_driver, 0, 0xffffffff);
323
324         device_create_file(dev, &dev_attr_vendor_id);
325         device_create_file(dev, &dev_attr_product_id);
326
327         return 0;
328 };
329
330 /**
331  *      i2o_exec_remove - Called on I2O device removal
332  *      @dev: I2O device which was removed
333  *
334  *      Unregisters event notification from Executive I2O device.
335  *
336  *      Returns 0 on success.
337  */
338 static int i2o_exec_remove(struct device *dev)
339 {
340         device_remove_file(dev, &dev_attr_product_id);
341         device_remove_file(dev, &dev_attr_vendor_id);
342
343         i2o_event_register(to_i2o_device(dev), &i2o_exec_driver, 0, 0);
344
345         return 0;
346 };
347
348 /**
349  *      i2o_exec_lct_modified - Called on LCT NOTIFY reply
350  *      @c: I2O controller on which the LCT has modified
351  *
352  *      This function handles asynchronus LCT NOTIFY replies. It parses the
353  *      new LCT and if the buffer for the LCT was to small sends a LCT NOTIFY
354  *      again, otherwise send LCT NOTIFY to get informed on next LCT change.
355  */
356 static void i2o_exec_lct_modified(struct i2o_controller *c)
357 {
358         u32 change_ind = 0;
359
360         if (i2o_device_parse_lct(c) != -EAGAIN)
361                 change_ind = c->lct->change_ind + 1;
362
363 #ifdef CONFIG_I2O_LCT_NOTIFY_ON_CHANGES
364         i2o_exec_lct_notify(c, change_ind);
365 #endif
366 };
367
368 /**
369  *      i2o_exec_reply -  I2O Executive reply handler
370  *      @c: I2O controller from which the reply comes
371  *      @m: message id
372  *      @msg: pointer to the I2O reply message
373  *
374  *      This function is always called from interrupt context. If a POST WAIT
375  *      reply was received, pass it to the complete function. If a LCT NOTIFY
376  *      reply was received, a new event is created to handle the update.
377  *
378  *      Returns 0 on success and if the reply should not be flushed or > 0
379  *      on success and if the reply should be flushed. Returns negative error
380  *      code on failure and if the reply should be flushed.
381  */
382 static int i2o_exec_reply(struct i2o_controller *c, u32 m,
383                           struct i2o_message *msg)
384 {
385         u32 context;
386
387         if (le32_to_cpu(msg->u.head[0]) & MSG_FAIL) {
388                 struct i2o_message __iomem *pmsg;
389                 u32 pm;
390
391                 /*
392                  * If Fail bit is set we must take the transaction context of
393                  * the preserved message to find the right request again.
394                  */
395
396                 pm = le32_to_cpu(msg->body[3]);
397                 pmsg = i2o_msg_in_to_virt(c, pm);
398                 context = readl(&pmsg->u.s.tcntxt);
399
400                 i2o_report_status(KERN_INFO, "i2o_core", msg);
401
402                 /* Release the preserved msg */
403                 i2o_msg_nop_mfa(c, pm);
404         } else
405                 context = le32_to_cpu(msg->u.s.tcntxt);
406
407         if (context & 0x80000000)
408                 return i2o_msg_post_wait_complete(c, m, msg, context);
409
410         if ((le32_to_cpu(msg->u.head[1]) >> 24) == I2O_CMD_LCT_NOTIFY) {
411                 struct work_struct *work;
412
413                 pr_debug("%s: LCT notify received\n", c->name);
414
415                 work = kmalloc(sizeof(*work), GFP_ATOMIC);
416                 if (!work)
417                         return -ENOMEM;
418
419                 INIT_WORK(work, (void (*)(void *))i2o_exec_lct_modified, c);
420                 queue_work(i2o_exec_driver.event_queue, work);
421                 return 1;
422         }
423
424         /*
425          * If this happens, we want to dump the message to the syslog so
426          * it can be sent back to the card manufacturer by the end user
427          * to aid in debugging.
428          *
429          */
430         printk(KERN_WARNING "%s: Unsolicited message reply sent to core!"
431                "Message dumped to syslog\n", c->name);
432         i2o_dump_message(msg);
433
434         return -EFAULT;
435 }
436
437 /**
438  *      i2o_exec_event - Event handling function
439  *      @evt: Event which occurs
440  *
441  *      Handles events send by the Executive device. At the moment does not do
442  *      anything useful.
443  */
444 static void i2o_exec_event(struct i2o_event *evt)
445 {
446         if (likely(evt->i2o_dev))
447                 osm_debug("Event received from device: %d\n",
448                           evt->i2o_dev->lct_data.tid);
449         kfree(evt);
450 };
451
452 /**
453  *      i2o_exec_lct_get - Get the IOP's Logical Configuration Table
454  *      @c: I2O controller from which the LCT should be fetched
455  *
456  *      Send a LCT NOTIFY request to the controller, and wait
457  *      I2O_TIMEOUT_LCT_GET seconds until arrival of response. If the LCT is
458  *      to large, retry it.
459  *
460  *      Returns 0 on success or negative error code on failure.
461  */
462 int i2o_exec_lct_get(struct i2o_controller *c)
463 {
464         struct i2o_message *msg;
465         int i = 0;
466         int rc = -EAGAIN;
467
468         for (i = 1; i <= I2O_LCT_GET_TRIES; i++) {
469                 msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
470                 if (IS_ERR(msg))
471                         return PTR_ERR(msg);
472
473                 msg->u.head[0] =
474                     cpu_to_le32(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_6);
475                 msg->u.head[1] =
476                     cpu_to_le32(I2O_CMD_LCT_NOTIFY << 24 | HOST_TID << 12 |
477                                 ADAPTER_TID);
478                 msg->body[0] = cpu_to_le32(0xffffffff);
479                 msg->body[1] = cpu_to_le32(0x00000000);
480                 msg->body[2] = cpu_to_le32(0xd0000000 | c->dlct.len);
481                 msg->body[3] = cpu_to_le32(c->dlct.phys);
482
483                 rc = i2o_msg_post_wait(c, msg, I2O_TIMEOUT_LCT_GET);
484                 if (rc < 0)
485                         break;
486
487                 rc = i2o_device_parse_lct(c);
488                 if (rc != -EAGAIN)
489                         break;
490         }
491
492         return rc;
493 }
494
495 /**
496  *      i2o_exec_lct_notify - Send a asynchronus LCT NOTIFY request
497  *      @c: I2O controller to which the request should be send
498  *      @change_ind: change indicator
499  *
500  *      This function sends a LCT NOTIFY request to the I2O controller with
501  *      the change indicator change_ind. If the change_ind == 0 the controller
502  *      replies immediately after the request. If change_ind > 0 the reply is
503  *      send after change indicator of the LCT is > change_ind.
504  */
505 static int i2o_exec_lct_notify(struct i2o_controller *c, u32 change_ind)
506 {
507         i2o_status_block *sb = c->status_block.virt;
508         struct device *dev;
509         struct i2o_message *msg;
510
511         down(&c->lct_lock);
512
513         dev = &c->pdev->dev;
514
515         if (i2o_dma_realloc
516             (dev, &c->dlct, le32_to_cpu(sb->expected_lct_size), GFP_KERNEL))
517                 return -ENOMEM;
518
519         msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
520         if (IS_ERR(msg))
521                 return PTR_ERR(msg);
522
523         msg->u.head[0] = cpu_to_le32(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_6);
524         msg->u.head[1] = cpu_to_le32(I2O_CMD_LCT_NOTIFY << 24 | HOST_TID << 12 |
525                                      ADAPTER_TID);
526         msg->u.s.icntxt = cpu_to_le32(i2o_exec_driver.context);
527         msg->u.s.tcntxt = cpu_to_le32(0x00000000);
528         msg->body[0] = cpu_to_le32(0xffffffff);
529         msg->body[1] = cpu_to_le32(change_ind);
530         msg->body[2] = cpu_to_le32(0xd0000000 | c->dlct.len);
531         msg->body[3] = cpu_to_le32(c->dlct.phys);
532
533         i2o_msg_post(c, msg);
534
535         up(&c->lct_lock);
536
537         return 0;
538 };
539
540 /* Exec OSM driver struct */
541 struct i2o_driver i2o_exec_driver = {
542         .name = OSM_NAME,
543         .reply = i2o_exec_reply,
544         .event = i2o_exec_event,
545         .classes = i2o_exec_class_id,
546         .driver = {
547                    .probe = i2o_exec_probe,
548                    .remove = i2o_exec_remove,
549                    },
550 };
551
552 /**
553  *      i2o_exec_init - Registers the Exec OSM
554  *
555  *      Registers the Exec OSM in the I2O core.
556  *
557  *      Returns 0 on success or negative error code on failure.
558  */
559 int __init i2o_exec_init(void)
560 {
561         return i2o_driver_register(&i2o_exec_driver);
562 };
563
564 /**
565  *      i2o_exec_exit - Removes the Exec OSM
566  *
567  *      Unregisters the Exec OSM from the I2O core.
568  */
569 void __exit i2o_exec_exit(void)
570 {
571         i2o_driver_unregister(&i2o_exec_driver);
572 };
573
574 EXPORT_SYMBOL(i2o_msg_post_wait_mem);
575 EXPORT_SYMBOL(i2o_exec_lct_get);