11da177e4SLinus Torvalds 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * IBM ASM Service Processor Device Driver 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 61da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 71da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 81da177e4SLinus Torvalds * (at your option) any later version. 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, 111da177e4SLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of 121da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 131da177e4SLinus Torvalds * GNU General Public License for more details. 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License 161da177e4SLinus Torvalds * along with this program; if not, write to the Free Software 171da177e4SLinus Torvalds * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds * Copyright (C) IBM Corporation, 2004 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * Author: Max Asb�ck <amax@us.ibm.com> 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds */ 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds #include "ibmasm.h" 2688187605SMax Asbock #include "lowlevel.h" 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds static void exec_next_command(struct service_processor *sp); 291da177e4SLinus Torvalds 3088187605SMax Asbock static atomic_t command_count = ATOMIC_INIT(0); 311da177e4SLinus Torvalds 3288187605SMax Asbock struct command *ibmasm_new_command(struct service_processor *sp, size_t buffer_size) 331da177e4SLinus Torvalds { 341da177e4SLinus Torvalds struct command *cmd; 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds if (buffer_size > IBMASM_CMD_MAX_BUFFER_SIZE) 371da177e4SLinus Torvalds return NULL; 381da177e4SLinus Torvalds 39dd00cc48SYoann Padioleau cmd = kzalloc(sizeof(struct command), GFP_KERNEL); 401da177e4SLinus Torvalds if (cmd == NULL) 411da177e4SLinus Torvalds return NULL; 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds 44dd00cc48SYoann Padioleau cmd->buffer = kzalloc(buffer_size, GFP_KERNEL); 451da177e4SLinus Torvalds if (cmd->buffer == NULL) { 461da177e4SLinus Torvalds kfree(cmd); 471da177e4SLinus Torvalds return NULL; 481da177e4SLinus Torvalds } 491da177e4SLinus Torvalds cmd->buffer_size = buffer_size; 501da177e4SLinus Torvalds 51*a045171fSGreg Kroah-Hartman kref_init(&cmd->kref); 5288187605SMax Asbock cmd->lock = &sp->lock; 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds cmd->status = IBMASM_CMD_PENDING; 551da177e4SLinus Torvalds init_waitqueue_head(&cmd->wait); 561da177e4SLinus Torvalds INIT_LIST_HEAD(&cmd->queue_node); 571da177e4SLinus Torvalds 5888187605SMax Asbock atomic_inc(&command_count); 5988187605SMax Asbock dbg("command count: %d\n", atomic_read(&command_count)); 6088187605SMax Asbock 611da177e4SLinus Torvalds return cmd; 621da177e4SLinus Torvalds } 631da177e4SLinus Torvalds 64*a045171fSGreg Kroah-Hartman void ibmasm_free_command(struct kref *kref) 651da177e4SLinus Torvalds { 66*a045171fSGreg Kroah-Hartman struct command *cmd = to_command(kref); 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds list_del(&cmd->queue_node); 6988187605SMax Asbock atomic_dec(&command_count); 7088187605SMax Asbock dbg("command count: %d\n", atomic_read(&command_count)); 711da177e4SLinus Torvalds kfree(cmd->buffer); 721da177e4SLinus Torvalds kfree(cmd); 731da177e4SLinus Torvalds } 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds static void enqueue_command(struct service_processor *sp, struct command *cmd) 761da177e4SLinus Torvalds { 771da177e4SLinus Torvalds list_add_tail(&cmd->queue_node, &sp->command_queue); 781da177e4SLinus Torvalds } 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds static struct command *dequeue_command(struct service_processor *sp) 811da177e4SLinus Torvalds { 821da177e4SLinus Torvalds struct command *cmd; 831da177e4SLinus Torvalds struct list_head *next; 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds if (list_empty(&sp->command_queue)) 861da177e4SLinus Torvalds return NULL; 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds next = sp->command_queue.next; 891da177e4SLinus Torvalds list_del_init(next); 901da177e4SLinus Torvalds cmd = list_entry(next, struct command, queue_node); 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds return cmd; 931da177e4SLinus Torvalds } 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds static inline void do_exec_command(struct service_processor *sp) 961da177e4SLinus Torvalds { 9788187605SMax Asbock char tsbuf[32]; 9888187605SMax Asbock 9988187605SMax Asbock dbg("%s:%d at %s\n", __FUNCTION__, __LINE__, get_timestamp(tsbuf)); 10088187605SMax Asbock 1011da177e4SLinus Torvalds if (ibmasm_send_i2o_message(sp)) { 1021da177e4SLinus Torvalds sp->current_command->status = IBMASM_CMD_FAILED; 10388187605SMax Asbock wake_up(&sp->current_command->wait); 10488187605SMax Asbock command_put(sp->current_command); 1051da177e4SLinus Torvalds exec_next_command(sp); 1061da177e4SLinus Torvalds } 1071da177e4SLinus Torvalds } 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds /** 1101da177e4SLinus Torvalds * exec_command 1111da177e4SLinus Torvalds * send a command to a service processor 1121da177e4SLinus Torvalds * Commands are executed sequentially. One command (sp->current_command) 1131da177e4SLinus Torvalds * is sent to the service processor. Once the interrupt handler gets a 1141da177e4SLinus Torvalds * message of type command_response, the message is copied into 1151da177e4SLinus Torvalds * the current commands buffer, 1161da177e4SLinus Torvalds */ 1171da177e4SLinus Torvalds void ibmasm_exec_command(struct service_processor *sp, struct command *cmd) 1181da177e4SLinus Torvalds { 1191da177e4SLinus Torvalds unsigned long flags; 12088187605SMax Asbock char tsbuf[32]; 12188187605SMax Asbock 12288187605SMax Asbock dbg("%s:%d at %s\n", __FUNCTION__, __LINE__, get_timestamp(tsbuf)); 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds spin_lock_irqsave(&sp->lock, flags); 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds if (!sp->current_command) { 1271da177e4SLinus Torvalds sp->current_command = cmd; 12888187605SMax Asbock command_get(sp->current_command); 1291da177e4SLinus Torvalds spin_unlock_irqrestore(&sp->lock, flags); 1301da177e4SLinus Torvalds do_exec_command(sp); 1311da177e4SLinus Torvalds } else { 1321da177e4SLinus Torvalds enqueue_command(sp, cmd); 1331da177e4SLinus Torvalds spin_unlock_irqrestore(&sp->lock, flags); 1341da177e4SLinus Torvalds } 1351da177e4SLinus Torvalds } 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds static void exec_next_command(struct service_processor *sp) 1381da177e4SLinus Torvalds { 1391da177e4SLinus Torvalds unsigned long flags; 14088187605SMax Asbock char tsbuf[32]; 1411da177e4SLinus Torvalds 14288187605SMax Asbock dbg("%s:%d at %s\n", __FUNCTION__, __LINE__, get_timestamp(tsbuf)); 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds spin_lock_irqsave(&sp->lock, flags); 1451da177e4SLinus Torvalds sp->current_command = dequeue_command(sp); 1461da177e4SLinus Torvalds if (sp->current_command) { 1471da177e4SLinus Torvalds command_get(sp->current_command); 1481da177e4SLinus Torvalds spin_unlock_irqrestore(&sp->lock, flags); 1491da177e4SLinus Torvalds do_exec_command(sp); 1501da177e4SLinus Torvalds } else { 1511da177e4SLinus Torvalds spin_unlock_irqrestore(&sp->lock, flags); 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds /** 1561da177e4SLinus Torvalds * Sleep until a command has failed or a response has been received 1571da177e4SLinus Torvalds * and the command status been updated by the interrupt handler. 1581da177e4SLinus Torvalds * (see receive_response). 1591da177e4SLinus Torvalds */ 1601da177e4SLinus Torvalds void ibmasm_wait_for_response(struct command *cmd, int timeout) 1611da177e4SLinus Torvalds { 1621da177e4SLinus Torvalds wait_event_interruptible_timeout(cmd->wait, 1631da177e4SLinus Torvalds cmd->status == IBMASM_CMD_COMPLETE || 1641da177e4SLinus Torvalds cmd->status == IBMASM_CMD_FAILED, 1651da177e4SLinus Torvalds timeout * HZ); 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds /** 1691da177e4SLinus Torvalds * receive_command_response 1701da177e4SLinus Torvalds * called by the interrupt handler when a dot command of type command_response 1711da177e4SLinus Torvalds * was received. 1721da177e4SLinus Torvalds */ 1731da177e4SLinus Torvalds void ibmasm_receive_command_response(struct service_processor *sp, void *response, size_t size) 1741da177e4SLinus Torvalds { 1751da177e4SLinus Torvalds struct command *cmd = sp->current_command; 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds if (!sp->current_command) 1781da177e4SLinus Torvalds return; 1791da177e4SLinus Torvalds 18088187605SMax Asbock memcpy_fromio(cmd->buffer, response, min(size, cmd->buffer_size)); 1811da177e4SLinus Torvalds cmd->status = IBMASM_CMD_COMPLETE; 18288187605SMax Asbock wake_up(&sp->current_command->wait); 18388187605SMax Asbock command_put(sp->current_command); 1841da177e4SLinus Torvalds exec_next_command(sp); 1851da177e4SLinus Torvalds } 186