/*1* Executive OSM2*3* Copyright (C) 1999-2002 Red Hat Software4*5* Written by Alan Cox, Building Number Three Ltd6*7* This program is free software; you can redistribute it and/or modify it8* under the terms of the GNU General Public License as published by the9* Free Software Foundation; either version 2 of the License, or (at your10* option) any later version.11*12* A lot of the I2O message side code from this is taken from the Red13* Creek RCPCI45 adapter driver by Red Creek Communications14*15* Fixes/additions:16* Philipp Rumpf17* Juha Sievänen <[email protected]>18* Auvo Häkkinen <[email protected]>19* Deepak Saxena <[email protected]>20* Boji T Kannanthanam <[email protected]>21* Alan Cox <[email protected]>:22* Ported to Linux 2.5.23* Markus Lidel <[email protected]>:24* Minor fixes for 2.6.25* Markus Lidel <[email protected]>:26* Support for sysfs included.27*/2829#include <linux/module.h>30#include <linux/i2o.h>31#include <linux/delay.h>32#include <linux/workqueue.h>33#include <linux/string.h>34#include <linux/slab.h>35#include <linux/sched.h> /* wait_event_interruptible_timeout() needs this */36#include <asm/param.h> /* HZ */37#include "core.h"3839#define OSM_NAME "exec-osm"4041struct i2o_driver i2o_exec_driver;4243/* global wait list for POST WAIT */44static LIST_HEAD(i2o_exec_wait_list);4546/* Wait struct needed for POST WAIT */47struct i2o_exec_wait {48wait_queue_head_t *wq; /* Pointer to Wait queue */49struct i2o_dma dma; /* DMA buffers to free on failure */50u32 tcntxt; /* transaction context from reply */51int complete; /* 1 if reply received otherwise 0 */52u32 m; /* message id */53struct i2o_message *msg; /* pointer to the reply message */54struct list_head list; /* node in global wait list */55spinlock_t lock; /* lock before modifying */56};5758/* Work struct needed to handle LCT NOTIFY replies */59struct i2o_exec_lct_notify_work {60struct work_struct work; /* work struct */61struct i2o_controller *c; /* controller on which the LCT NOTIFY62was received */63};6465/* Exec OSM class handling definition */66static struct i2o_class_id i2o_exec_class_id[] = {67{I2O_CLASS_EXECUTIVE},68{I2O_CLASS_END}69};7071/**72* i2o_exec_wait_alloc - Allocate a i2o_exec_wait struct an initialize it73*74* Allocate the i2o_exec_wait struct and initialize the wait.75*76* Returns i2o_exec_wait pointer on success or negative error code on77* failure.78*/79static struct i2o_exec_wait *i2o_exec_wait_alloc(void)80{81struct i2o_exec_wait *wait;8283wait = kzalloc(sizeof(*wait), GFP_KERNEL);84if (!wait)85return NULL;8687INIT_LIST_HEAD(&wait->list);88spin_lock_init(&wait->lock);8990return wait;91};9293/**94* i2o_exec_wait_free - Free an i2o_exec_wait struct95* @wait: I2O wait data which should be cleaned up96*/97static void i2o_exec_wait_free(struct i2o_exec_wait *wait)98{99kfree(wait);100};101102/**103* i2o_msg_post_wait_mem - Post and wait a message with DMA buffers104* @c: controller105* @msg: message to post106* @timeout: time in seconds to wait107* @dma: i2o_dma struct of the DMA buffer to free on failure108*109* This API allows an OSM to post a message and then be told whether or110* not the system received a successful reply. If the message times out111* then the value '-ETIMEDOUT' is returned. This is a special case. In112* this situation the message may (should) complete at an indefinite time113* in the future. When it completes it will use the memory buffer114* attached to the request. If -ETIMEDOUT is returned then the memory115* buffer must not be freed. Instead the event completion will free them116* for you. In all other cases the buffer are your problem.117*118* Returns 0 on success, negative error code on timeout or positive error119* code from reply.120*/121int i2o_msg_post_wait_mem(struct i2o_controller *c, struct i2o_message *msg,122unsigned long timeout, struct i2o_dma *dma)123{124DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);125struct i2o_exec_wait *wait;126static u32 tcntxt = 0x80000000;127unsigned long flags;128int rc = 0;129130wait = i2o_exec_wait_alloc();131if (!wait) {132i2o_msg_nop(c, msg);133return -ENOMEM;134}135136if (tcntxt == 0xffffffff)137tcntxt = 0x80000000;138139if (dma)140wait->dma = *dma;141142/*143* Fill in the message initiator context and transaction context.144* We will only use transaction contexts >= 0x80000000 for POST WAIT,145* so we could find a POST WAIT reply easier in the reply handler.146*/147msg->u.s.icntxt = cpu_to_le32(i2o_exec_driver.context);148wait->tcntxt = tcntxt++;149msg->u.s.tcntxt = cpu_to_le32(wait->tcntxt);150151wait->wq = &wq;152/*153* we add elements to the head, because if a entry in the list will154* never be removed, we have to iterate over it every time155*/156list_add(&wait->list, &i2o_exec_wait_list);157158/*159* Post the message to the controller. At some point later it will160* return. If we time out before it returns then complete will be zero.161*/162i2o_msg_post(c, msg);163164wait_event_interruptible_timeout(wq, wait->complete, timeout * HZ);165166spin_lock_irqsave(&wait->lock, flags);167168wait->wq = NULL;169170if (wait->complete)171rc = le32_to_cpu(wait->msg->body[0]) >> 24;172else {173/*174* We cannot remove it now. This is important. When it does175* terminate (which it must do if the controller has not176* died...) then it will otherwise scribble on stuff.177*178* FIXME: try abort message179*/180if (dma)181dma->virt = NULL;182183rc = -ETIMEDOUT;184}185186spin_unlock_irqrestore(&wait->lock, flags);187188if (rc != -ETIMEDOUT) {189i2o_flush_reply(c, wait->m);190i2o_exec_wait_free(wait);191}192193return rc;194};195196/**197* i2o_msg_post_wait_complete - Reply to a i2o_msg_post request from IOP198* @c: I2O controller which answers199* @m: message id200* @msg: pointer to the I2O reply message201* @context: transaction context of request202*203* This function is called in interrupt context only. If the reply reached204* before the timeout, the i2o_exec_wait struct is filled with the message205* and the task will be waked up. The task is now responsible for returning206* the message m back to the controller! If the message reaches us after207* the timeout clean up the i2o_exec_wait struct (including allocated208* DMA buffer).209*210* Return 0 on success and if the message m should not be given back to the211* I2O controller, or >0 on success and if the message should be given back212* afterwords. Returns negative error code on failure. In this case the213* message must also be given back to the controller.214*/215static int i2o_msg_post_wait_complete(struct i2o_controller *c, u32 m,216struct i2o_message *msg, u32 context)217{218struct i2o_exec_wait *wait, *tmp;219unsigned long flags;220int rc = 1;221222/*223* We need to search through the i2o_exec_wait_list to see if the given224* message is still outstanding. If not, it means that the IOP took225* longer to respond to the message than we had allowed and timer has226* already expired. Not much we can do about that except log it for227* debug purposes, increase timeout, and recompile.228*/229list_for_each_entry_safe(wait, tmp, &i2o_exec_wait_list, list) {230if (wait->tcntxt == context) {231spin_lock_irqsave(&wait->lock, flags);232233list_del(&wait->list);234235wait->m = m;236wait->msg = msg;237wait->complete = 1;238239if (wait->wq)240rc = 0;241else242rc = -1;243244spin_unlock_irqrestore(&wait->lock, flags);245246if (rc) {247struct device *dev;248249dev = &c->pdev->dev;250251pr_debug("%s: timedout reply received!\n",252c->name);253i2o_dma_free(dev, &wait->dma);254i2o_exec_wait_free(wait);255} else256wake_up_interruptible(wait->wq);257258return rc;259}260}261262osm_warn("%s: Bogus reply in POST WAIT (tr-context: %08x)!\n", c->name,263context);264265return -1;266};267268/**269* i2o_exec_show_vendor_id - Displays Vendor ID of controller270* @d: device of which the Vendor ID should be displayed271* @attr: device_attribute to display272* @buf: buffer into which the Vendor ID should be printed273*274* Returns number of bytes printed into buffer.275*/276static ssize_t i2o_exec_show_vendor_id(struct device *d,277struct device_attribute *attr, char *buf)278{279struct i2o_device *dev = to_i2o_device(d);280u16 id;281282if (!i2o_parm_field_get(dev, 0x0000, 0, &id, 2)) {283sprintf(buf, "0x%04x", le16_to_cpu(id));284return strlen(buf) + 1;285}286287return 0;288};289290/**291* i2o_exec_show_product_id - Displays Product ID of controller292* @d: device of which the Product ID should be displayed293* @attr: device_attribute to display294* @buf: buffer into which the Product ID should be printed295*296* Returns number of bytes printed into buffer.297*/298static ssize_t i2o_exec_show_product_id(struct device *d,299struct device_attribute *attr,300char *buf)301{302struct i2o_device *dev = to_i2o_device(d);303u16 id;304305if (!i2o_parm_field_get(dev, 0x0000, 1, &id, 2)) {306sprintf(buf, "0x%04x", le16_to_cpu(id));307return strlen(buf) + 1;308}309310return 0;311};312313/* Exec-OSM device attributes */314static DEVICE_ATTR(vendor_id, S_IRUGO, i2o_exec_show_vendor_id, NULL);315static DEVICE_ATTR(product_id, S_IRUGO, i2o_exec_show_product_id, NULL);316317/**318* i2o_exec_probe - Called if a new I2O device (executive class) appears319* @dev: I2O device which should be probed320*321* Registers event notification for every event from Executive device. The322* return is always 0, because we want all devices of class Executive.323*324* Returns 0 on success.325*/326static int i2o_exec_probe(struct device *dev)327{328struct i2o_device *i2o_dev = to_i2o_device(dev);329int rc;330331rc = i2o_event_register(i2o_dev, &i2o_exec_driver, 0, 0xffffffff);332if (rc) goto err_out;333334rc = device_create_file(dev, &dev_attr_vendor_id);335if (rc) goto err_evtreg;336rc = device_create_file(dev, &dev_attr_product_id);337if (rc) goto err_vid;338339i2o_dev->iop->exec = i2o_dev;340341return 0;342343err_vid:344device_remove_file(dev, &dev_attr_vendor_id);345err_evtreg:346i2o_event_register(to_i2o_device(dev), &i2o_exec_driver, 0, 0);347err_out:348return rc;349};350351/**352* i2o_exec_remove - Called on I2O device removal353* @dev: I2O device which was removed354*355* Unregisters event notification from Executive I2O device.356*357* Returns 0 on success.358*/359static int i2o_exec_remove(struct device *dev)360{361device_remove_file(dev, &dev_attr_product_id);362device_remove_file(dev, &dev_attr_vendor_id);363364i2o_event_register(to_i2o_device(dev), &i2o_exec_driver, 0, 0);365366return 0;367};368369#ifdef CONFIG_I2O_LCT_NOTIFY_ON_CHANGES370/**371* i2o_exec_lct_notify - Send a asynchronus LCT NOTIFY request372* @c: I2O controller to which the request should be send373* @change_ind: change indicator374*375* This function sends a LCT NOTIFY request to the I2O controller with376* the change indicator change_ind. If the change_ind == 0 the controller377* replies immediately after the request. If change_ind > 0 the reply is378* send after change indicator of the LCT is > change_ind.379*/380static int i2o_exec_lct_notify(struct i2o_controller *c, u32 change_ind)381{382i2o_status_block *sb = c->status_block.virt;383struct device *dev;384struct i2o_message *msg;385386mutex_lock(&c->lct_lock);387388dev = &c->pdev->dev;389390if (i2o_dma_realloc(dev, &c->dlct,391le32_to_cpu(sb->expected_lct_size))) {392mutex_unlock(&c->lct_lock);393return -ENOMEM;394}395396msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);397if (IS_ERR(msg)) {398mutex_unlock(&c->lct_lock);399return PTR_ERR(msg);400}401402msg->u.head[0] = cpu_to_le32(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_6);403msg->u.head[1] = cpu_to_le32(I2O_CMD_LCT_NOTIFY << 24 | HOST_TID << 12 |404ADAPTER_TID);405msg->u.s.icntxt = cpu_to_le32(i2o_exec_driver.context);406msg->u.s.tcntxt = cpu_to_le32(0x00000000);407msg->body[0] = cpu_to_le32(0xffffffff);408msg->body[1] = cpu_to_le32(change_ind);409msg->body[2] = cpu_to_le32(0xd0000000 | c->dlct.len);410msg->body[3] = cpu_to_le32(c->dlct.phys);411412i2o_msg_post(c, msg);413414mutex_unlock(&c->lct_lock);415416return 0;417}418#endif419420/**421* i2o_exec_lct_modified - Called on LCT NOTIFY reply422* @_work: work struct for a specific controller423*424* This function handles asynchronus LCT NOTIFY replies. It parses the425* new LCT and if the buffer for the LCT was to small sends a LCT NOTIFY426* again, otherwise send LCT NOTIFY to get informed on next LCT change.427*/428static void i2o_exec_lct_modified(struct work_struct *_work)429{430struct i2o_exec_lct_notify_work *work =431container_of(_work, struct i2o_exec_lct_notify_work, work);432u32 change_ind = 0;433struct i2o_controller *c = work->c;434435kfree(work);436437if (i2o_device_parse_lct(c) != -EAGAIN)438change_ind = c->lct->change_ind + 1;439440#ifdef CONFIG_I2O_LCT_NOTIFY_ON_CHANGES441i2o_exec_lct_notify(c, change_ind);442#endif443};444445/**446* i2o_exec_reply - I2O Executive reply handler447* @c: I2O controller from which the reply comes448* @m: message id449* @msg: pointer to the I2O reply message450*451* This function is always called from interrupt context. If a POST WAIT452* reply was received, pass it to the complete function. If a LCT NOTIFY453* reply was received, a new event is created to handle the update.454*455* Returns 0 on success and if the reply should not be flushed or > 0456* on success and if the reply should be flushed. Returns negative error457* code on failure and if the reply should be flushed.458*/459static int i2o_exec_reply(struct i2o_controller *c, u32 m,460struct i2o_message *msg)461{462u32 context;463464if (le32_to_cpu(msg->u.head[0]) & MSG_FAIL) {465struct i2o_message __iomem *pmsg;466u32 pm;467468/*469* If Fail bit is set we must take the transaction context of470* the preserved message to find the right request again.471*/472473pm = le32_to_cpu(msg->body[3]);474pmsg = i2o_msg_in_to_virt(c, pm);475context = readl(&pmsg->u.s.tcntxt);476477i2o_report_status(KERN_INFO, "i2o_core", msg);478479/* Release the preserved msg */480i2o_msg_nop_mfa(c, pm);481} else482context = le32_to_cpu(msg->u.s.tcntxt);483484if (context & 0x80000000)485return i2o_msg_post_wait_complete(c, m, msg, context);486487if ((le32_to_cpu(msg->u.head[1]) >> 24) == I2O_CMD_LCT_NOTIFY) {488struct i2o_exec_lct_notify_work *work;489490pr_debug("%s: LCT notify received\n", c->name);491492work = kmalloc(sizeof(*work), GFP_ATOMIC);493if (!work)494return -ENOMEM;495496work->c = c;497498INIT_WORK(&work->work, i2o_exec_lct_modified);499queue_work(i2o_exec_driver.event_queue, &work->work);500return 1;501}502503/*504* If this happens, we want to dump the message to the syslog so505* it can be sent back to the card manufacturer by the end user506* to aid in debugging.507*508*/509printk(KERN_WARNING "%s: Unsolicited message reply sent to core!"510"Message dumped to syslog\n", c->name);511i2o_dump_message(msg);512513return -EFAULT;514}515516/**517* i2o_exec_event - Event handling function518* @work: Work item in occurring event519*520* Handles events send by the Executive device. At the moment does not do521* anything useful.522*/523static void i2o_exec_event(struct work_struct *work)524{525struct i2o_event *evt = container_of(work, struct i2o_event, work);526527if (likely(evt->i2o_dev))528osm_debug("Event received from device: %d\n",529evt->i2o_dev->lct_data.tid);530kfree(evt);531};532533/**534* i2o_exec_lct_get - Get the IOP's Logical Configuration Table535* @c: I2O controller from which the LCT should be fetched536*537* Send a LCT NOTIFY request to the controller, and wait538* I2O_TIMEOUT_LCT_GET seconds until arrival of response. If the LCT is539* to large, retry it.540*541* Returns 0 on success or negative error code on failure.542*/543int i2o_exec_lct_get(struct i2o_controller *c)544{545struct i2o_message *msg;546int i = 0;547int rc = -EAGAIN;548549for (i = 1; i <= I2O_LCT_GET_TRIES; i++) {550msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);551if (IS_ERR(msg))552return PTR_ERR(msg);553554msg->u.head[0] =555cpu_to_le32(EIGHT_WORD_MSG_SIZE | SGL_OFFSET_6);556msg->u.head[1] =557cpu_to_le32(I2O_CMD_LCT_NOTIFY << 24 | HOST_TID << 12 |558ADAPTER_TID);559msg->body[0] = cpu_to_le32(0xffffffff);560msg->body[1] = cpu_to_le32(0x00000000);561msg->body[2] = cpu_to_le32(0xd0000000 | c->dlct.len);562msg->body[3] = cpu_to_le32(c->dlct.phys);563564rc = i2o_msg_post_wait(c, msg, I2O_TIMEOUT_LCT_GET);565if (rc < 0)566break;567568rc = i2o_device_parse_lct(c);569if (rc != -EAGAIN)570break;571}572573return rc;574}575576/* Exec OSM driver struct */577struct i2o_driver i2o_exec_driver = {578.name = OSM_NAME,579.reply = i2o_exec_reply,580.event = i2o_exec_event,581.classes = i2o_exec_class_id,582.driver = {583.probe = i2o_exec_probe,584.remove = i2o_exec_remove,585},586};587588/**589* i2o_exec_init - Registers the Exec OSM590*591* Registers the Exec OSM in the I2O core.592*593* Returns 0 on success or negative error code on failure.594*/595int __init i2o_exec_init(void)596{597return i2o_driver_register(&i2o_exec_driver);598};599600/**601* i2o_exec_exit - Removes the Exec OSM602*603* Unregisters the Exec OSM from the I2O core.604*/605void i2o_exec_exit(void)606{607i2o_driver_unregister(&i2o_exec_driver);608};609610EXPORT_SYMBOL(i2o_msg_post_wait_mem);611EXPORT_SYMBOL(i2o_exec_lct_get);612613614