1*1a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds 31da177e4SLinus Torvalds /* 41da177e4SLinus Torvalds * IBM ASM Service Processor Device Driver 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright (C) IBM Corporation, 2004 71da177e4SLinus Torvalds * 8d36b6910SAl Viro * Author: Max Asböck <amax@us.ibm.com> 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 11d43c36dcSAlexey Dobriyan #include <linux/sched.h> 125a0e3ad6STejun Heo #include <linux/slab.h> 131da177e4SLinus Torvalds #include "ibmasm.h" 1488187605SMax Asbock #include "lowlevel.h" 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds static void exec_next_command(struct service_processor *sp); 171da177e4SLinus Torvalds 1888187605SMax Asbock static atomic_t command_count = ATOMIC_INIT(0); 191da177e4SLinus Torvalds 2088187605SMax Asbock struct command *ibmasm_new_command(struct service_processor *sp, size_t buffer_size) 211da177e4SLinus Torvalds { 221da177e4SLinus Torvalds struct command *cmd; 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds if (buffer_size > IBMASM_CMD_MAX_BUFFER_SIZE) 251da177e4SLinus Torvalds return NULL; 261da177e4SLinus Torvalds 27dd00cc48SYoann Padioleau cmd = kzalloc(sizeof(struct command), GFP_KERNEL); 281da177e4SLinus Torvalds if (cmd == NULL) 291da177e4SLinus Torvalds return NULL; 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds 32dd00cc48SYoann Padioleau cmd->buffer = kzalloc(buffer_size, GFP_KERNEL); 331da177e4SLinus Torvalds if (cmd->buffer == NULL) { 341da177e4SLinus Torvalds kfree(cmd); 351da177e4SLinus Torvalds return NULL; 361da177e4SLinus Torvalds } 371da177e4SLinus Torvalds cmd->buffer_size = buffer_size; 381da177e4SLinus Torvalds 39a045171fSGreg Kroah-Hartman kref_init(&cmd->kref); 4088187605SMax Asbock cmd->lock = &sp->lock; 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds cmd->status = IBMASM_CMD_PENDING; 431da177e4SLinus Torvalds init_waitqueue_head(&cmd->wait); 441da177e4SLinus Torvalds INIT_LIST_HEAD(&cmd->queue_node); 451da177e4SLinus Torvalds 4688187605SMax Asbock atomic_inc(&command_count); 4788187605SMax Asbock dbg("command count: %d\n", atomic_read(&command_count)); 4888187605SMax Asbock 491da177e4SLinus Torvalds return cmd; 501da177e4SLinus Torvalds } 511da177e4SLinus Torvalds 52a045171fSGreg Kroah-Hartman void ibmasm_free_command(struct kref *kref) 531da177e4SLinus Torvalds { 54a045171fSGreg Kroah-Hartman struct command *cmd = to_command(kref); 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds list_del(&cmd->queue_node); 5788187605SMax Asbock atomic_dec(&command_count); 5888187605SMax Asbock dbg("command count: %d\n", atomic_read(&command_count)); 591da177e4SLinus Torvalds kfree(cmd->buffer); 601da177e4SLinus Torvalds kfree(cmd); 611da177e4SLinus Torvalds } 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds static void enqueue_command(struct service_processor *sp, struct command *cmd) 641da177e4SLinus Torvalds { 651da177e4SLinus Torvalds list_add_tail(&cmd->queue_node, &sp->command_queue); 661da177e4SLinus Torvalds } 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds static struct command *dequeue_command(struct service_processor *sp) 691da177e4SLinus Torvalds { 701da177e4SLinus Torvalds struct command *cmd; 711da177e4SLinus Torvalds struct list_head *next; 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds if (list_empty(&sp->command_queue)) 741da177e4SLinus Torvalds return NULL; 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds next = sp->command_queue.next; 771da177e4SLinus Torvalds list_del_init(next); 781da177e4SLinus Torvalds cmd = list_entry(next, struct command, queue_node); 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds return cmd; 811da177e4SLinus Torvalds } 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds static inline void do_exec_command(struct service_processor *sp) 841da177e4SLinus Torvalds { 8588187605SMax Asbock char tsbuf[32]; 8688187605SMax Asbock 876e574195SHarvey Harrison dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf)); 8888187605SMax Asbock 891da177e4SLinus Torvalds if (ibmasm_send_i2o_message(sp)) { 901da177e4SLinus Torvalds sp->current_command->status = IBMASM_CMD_FAILED; 9188187605SMax Asbock wake_up(&sp->current_command->wait); 9288187605SMax Asbock command_put(sp->current_command); 931da177e4SLinus Torvalds exec_next_command(sp); 941da177e4SLinus Torvalds } 951da177e4SLinus Torvalds } 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds /** 981da177e4SLinus Torvalds * exec_command 991da177e4SLinus Torvalds * send a command to a service processor 1001da177e4SLinus Torvalds * Commands are executed sequentially. One command (sp->current_command) 1011da177e4SLinus Torvalds * is sent to the service processor. Once the interrupt handler gets a 1021da177e4SLinus Torvalds * message of type command_response, the message is copied into 1031da177e4SLinus Torvalds * the current commands buffer, 1041da177e4SLinus Torvalds */ 1051da177e4SLinus Torvalds void ibmasm_exec_command(struct service_processor *sp, struct command *cmd) 1061da177e4SLinus Torvalds { 1071da177e4SLinus Torvalds unsigned long flags; 10888187605SMax Asbock char tsbuf[32]; 10988187605SMax Asbock 1106e574195SHarvey Harrison dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf)); 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds spin_lock_irqsave(&sp->lock, flags); 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds if (!sp->current_command) { 1151da177e4SLinus Torvalds sp->current_command = cmd; 11688187605SMax Asbock command_get(sp->current_command); 1171da177e4SLinus Torvalds spin_unlock_irqrestore(&sp->lock, flags); 1181da177e4SLinus Torvalds do_exec_command(sp); 1191da177e4SLinus Torvalds } else { 1201da177e4SLinus Torvalds enqueue_command(sp, cmd); 1211da177e4SLinus Torvalds spin_unlock_irqrestore(&sp->lock, flags); 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds } 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds static void exec_next_command(struct service_processor *sp) 1261da177e4SLinus Torvalds { 1271da177e4SLinus Torvalds unsigned long flags; 12888187605SMax Asbock char tsbuf[32]; 1291da177e4SLinus Torvalds 1306e574195SHarvey Harrison dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf)); 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds spin_lock_irqsave(&sp->lock, flags); 1331da177e4SLinus Torvalds sp->current_command = dequeue_command(sp); 1341da177e4SLinus Torvalds if (sp->current_command) { 1351da177e4SLinus Torvalds command_get(sp->current_command); 1361da177e4SLinus Torvalds spin_unlock_irqrestore(&sp->lock, flags); 1371da177e4SLinus Torvalds do_exec_command(sp); 1381da177e4SLinus Torvalds } else { 1391da177e4SLinus Torvalds spin_unlock_irqrestore(&sp->lock, flags); 1401da177e4SLinus Torvalds } 1411da177e4SLinus Torvalds } 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds /** 1441da177e4SLinus Torvalds * Sleep until a command has failed or a response has been received 1451da177e4SLinus Torvalds * and the command status been updated by the interrupt handler. 1461da177e4SLinus Torvalds * (see receive_response). 1471da177e4SLinus Torvalds */ 1481da177e4SLinus Torvalds void ibmasm_wait_for_response(struct command *cmd, int timeout) 1491da177e4SLinus Torvalds { 1501da177e4SLinus Torvalds wait_event_interruptible_timeout(cmd->wait, 1511da177e4SLinus Torvalds cmd->status == IBMASM_CMD_COMPLETE || 1521da177e4SLinus Torvalds cmd->status == IBMASM_CMD_FAILED, 1531da177e4SLinus Torvalds timeout * HZ); 1541da177e4SLinus Torvalds } 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds /** 1571da177e4SLinus Torvalds * receive_command_response 1581da177e4SLinus Torvalds * called by the interrupt handler when a dot command of type command_response 1591da177e4SLinus Torvalds * was received. 1601da177e4SLinus Torvalds */ 1611da177e4SLinus Torvalds void ibmasm_receive_command_response(struct service_processor *sp, void *response, size_t size) 1621da177e4SLinus Torvalds { 1631da177e4SLinus Torvalds struct command *cmd = sp->current_command; 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds if (!sp->current_command) 1661da177e4SLinus Torvalds return; 1671da177e4SLinus Torvalds 16888187605SMax Asbock memcpy_fromio(cmd->buffer, response, min(size, cmd->buffer_size)); 1691da177e4SLinus Torvalds cmd->status = IBMASM_CMD_COMPLETE; 17088187605SMax Asbock wake_up(&sp->current_command->wait); 17188187605SMax Asbock command_put(sp->current_command); 1721da177e4SLinus Torvalds exec_next_command(sp); 1731da177e4SLinus Torvalds } 174