Path: blob/master/arch/powerpc/platforms/iseries/mf.c
10820 views
/*1* Copyright (C) 2001 Troy D. Armstrong IBM Corporation2* Copyright (C) 2004-2005 Stephen Rothwell IBM Corporation3*4* This modules exists as an interface between a Linux secondary partition5* running on an iSeries and the primary partition's Virtual Service6* Processor (VSP) object. The VSP has final authority over powering on/off7* all partitions in the iSeries. It also provides miscellaneous low-level8* machine facility type operations.9*10*11* This program is free software; you can redistribute it and/or modify12* it under the terms of the GNU General Public License as published by13* the Free Software Foundation; either version 2 of the License, or14* (at your option) any later version.15*16* This program is distributed in the hope that it will be useful,17* but WITHOUT ANY WARRANTY; without even the implied warranty of18* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the19* GNU General Public License for more details.20*21* You should have received a copy of the GNU General Public License22* along with this program; if not, write to the Free Software23* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA24*/2526#include <linux/types.h>27#include <linux/errno.h>28#include <linux/kernel.h>29#include <linux/init.h>30#include <linux/completion.h>31#include <linux/delay.h>32#include <linux/proc_fs.h>33#include <linux/dma-mapping.h>34#include <linux/bcd.h>35#include <linux/rtc.h>36#include <linux/slab.h>3738#include <asm/time.h>39#include <asm/uaccess.h>40#include <asm/paca.h>41#include <asm/abs_addr.h>42#include <asm/firmware.h>43#include <asm/iseries/mf.h>44#include <asm/iseries/hv_lp_config.h>45#include <asm/iseries/hv_lp_event.h>46#include <asm/iseries/it_lp_queue.h>4748#include "setup.h"4950static int mf_initialized;5152/*53* This is the structure layout for the Machine Facilities LPAR event54* flows.55*/56struct vsp_cmd_data {57u64 token;58u16 cmd;59HvLpIndex lp_index;60u8 result_code;61u32 reserved;62union {63u64 state; /* GetStateOut */64u64 ipl_type; /* GetIplTypeOut, Function02SelectIplTypeIn */65u64 ipl_mode; /* GetIplModeOut, Function02SelectIplModeIn */66u64 page[4]; /* GetSrcHistoryIn */67u64 flag; /* GetAutoIplWhenPrimaryIplsOut,68SetAutoIplWhenPrimaryIplsIn,69WhiteButtonPowerOffIn,70Function08FastPowerOffIn,71IsSpcnRackPowerIncompleteOut */72struct {73u64 token;74u64 address_type;75u64 side;76u32 length;77u32 offset;78} kern; /* SetKernelImageIn, GetKernelImageIn,79SetKernelCmdLineIn, GetKernelCmdLineIn */80u32 length_out; /* GetKernelImageOut, GetKernelCmdLineOut */81u8 reserved[80];82} sub_data;83};8485struct vsp_rsp_data {86struct completion com;87struct vsp_cmd_data *response;88};8990struct alloc_data {91u16 size;92u16 type;93u32 count;94u16 reserved1;95u8 reserved2;96HvLpIndex target_lp;97};9899struct ce_msg_data;100101typedef void (*ce_msg_comp_hdlr)(void *token, struct ce_msg_data *vsp_cmd_rsp);102103struct ce_msg_comp_data {104ce_msg_comp_hdlr handler;105void *token;106};107108struct ce_msg_data {109u8 ce_msg[12];110char reserved[4];111struct ce_msg_comp_data *completion;112};113114struct io_mf_lp_event {115struct HvLpEvent hp_lp_event;116u16 subtype_result_code;117u16 reserved1;118u32 reserved2;119union {120struct alloc_data alloc;121struct ce_msg_data ce_msg;122struct vsp_cmd_data vsp_cmd;123} data;124};125126#define subtype_data(a, b, c, d) \127(((a) << 24) + ((b) << 16) + ((c) << 8) + (d))128129/*130* All outgoing event traffic is kept on a FIFO queue. The first131* pointer points to the one that is outstanding, and all new132* requests get stuck on the end. Also, we keep a certain number of133* preallocated pending events so that we can operate very early in134* the boot up sequence (before kmalloc is ready).135*/136struct pending_event {137struct pending_event *next;138struct io_mf_lp_event event;139MFCompleteHandler hdlr;140char dma_data[72];141unsigned dma_data_length;142unsigned remote_address;143};144static spinlock_t pending_event_spinlock;145static struct pending_event *pending_event_head;146static struct pending_event *pending_event_tail;147static struct pending_event *pending_event_avail;148#define PENDING_EVENT_PREALLOC_LEN 16149static struct pending_event pending_event_prealloc[PENDING_EVENT_PREALLOC_LEN];150151/*152* Put a pending event onto the available queue, so it can get reused.153* Attention! You must have the pending_event_spinlock before calling!154*/155static void free_pending_event(struct pending_event *ev)156{157if (ev != NULL) {158ev->next = pending_event_avail;159pending_event_avail = ev;160}161}162163/*164* Enqueue the outbound event onto the stack. If the queue was165* empty to begin with, we must also issue it via the Hypervisor166* interface. There is a section of code below that will touch167* the first stack pointer without the protection of the pending_event_spinlock.168* This is OK, because we know that nobody else will be modifying169* the first pointer when we do this.170*/171static int signal_event(struct pending_event *ev)172{173int rc = 0;174unsigned long flags;175int go = 1;176struct pending_event *ev1;177HvLpEvent_Rc hv_rc;178179/* enqueue the event */180if (ev != NULL) {181ev->next = NULL;182spin_lock_irqsave(&pending_event_spinlock, flags);183if (pending_event_head == NULL)184pending_event_head = ev;185else {186go = 0;187pending_event_tail->next = ev;188}189pending_event_tail = ev;190spin_unlock_irqrestore(&pending_event_spinlock, flags);191}192193/* send the event */194while (go) {195go = 0;196197/* any DMA data to send beforehand? */198if (pending_event_head->dma_data_length > 0)199HvCallEvent_dmaToSp(pending_event_head->dma_data,200pending_event_head->remote_address,201pending_event_head->dma_data_length,202HvLpDma_Direction_LocalToRemote);203204hv_rc = HvCallEvent_signalLpEvent(205&pending_event_head->event.hp_lp_event);206if (hv_rc != HvLpEvent_Rc_Good) {207printk(KERN_ERR "mf.c: HvCallEvent_signalLpEvent() "208"failed with %d\n", (int)hv_rc);209210spin_lock_irqsave(&pending_event_spinlock, flags);211ev1 = pending_event_head;212pending_event_head = pending_event_head->next;213if (pending_event_head != NULL)214go = 1;215spin_unlock_irqrestore(&pending_event_spinlock, flags);216217if (ev1 == ev)218rc = -EIO;219else if (ev1->hdlr != NULL)220(*ev1->hdlr)((void *)ev1->event.hp_lp_event.xCorrelationToken, -EIO);221222spin_lock_irqsave(&pending_event_spinlock, flags);223free_pending_event(ev1);224spin_unlock_irqrestore(&pending_event_spinlock, flags);225}226}227228return rc;229}230231/*232* Allocate a new pending_event structure, and initialize it.233*/234static struct pending_event *new_pending_event(void)235{236struct pending_event *ev = NULL;237HvLpIndex primary_lp = HvLpConfig_getPrimaryLpIndex();238unsigned long flags;239struct HvLpEvent *hev;240241spin_lock_irqsave(&pending_event_spinlock, flags);242if (pending_event_avail != NULL) {243ev = pending_event_avail;244pending_event_avail = pending_event_avail->next;245}246spin_unlock_irqrestore(&pending_event_spinlock, flags);247if (ev == NULL) {248ev = kmalloc(sizeof(struct pending_event), GFP_ATOMIC);249if (ev == NULL) {250printk(KERN_ERR "mf.c: unable to kmalloc %ld bytes\n",251sizeof(struct pending_event));252return NULL;253}254}255memset(ev, 0, sizeof(struct pending_event));256hev = &ev->event.hp_lp_event;257hev->flags = HV_LP_EVENT_VALID | HV_LP_EVENT_DO_ACK | HV_LP_EVENT_INT;258hev->xType = HvLpEvent_Type_MachineFac;259hev->xSourceLp = HvLpConfig_getLpIndex();260hev->xTargetLp = primary_lp;261hev->xSizeMinus1 = sizeof(ev->event) - 1;262hev->xRc = HvLpEvent_Rc_Good;263hev->xSourceInstanceId = HvCallEvent_getSourceLpInstanceId(primary_lp,264HvLpEvent_Type_MachineFac);265hev->xTargetInstanceId = HvCallEvent_getTargetLpInstanceId(primary_lp,266HvLpEvent_Type_MachineFac);267268return ev;269}270271static int __maybe_unused272signal_vsp_instruction(struct vsp_cmd_data *vsp_cmd)273{274struct pending_event *ev = new_pending_event();275int rc;276struct vsp_rsp_data response;277278if (ev == NULL)279return -ENOMEM;280281init_completion(&response.com);282response.response = vsp_cmd;283ev->event.hp_lp_event.xSubtype = 6;284ev->event.hp_lp_event.x.xSubtypeData =285subtype_data('M', 'F', 'V', 'I');286ev->event.data.vsp_cmd.token = (u64)&response;287ev->event.data.vsp_cmd.cmd = vsp_cmd->cmd;288ev->event.data.vsp_cmd.lp_index = HvLpConfig_getLpIndex();289ev->event.data.vsp_cmd.result_code = 0xFF;290ev->event.data.vsp_cmd.reserved = 0;291memcpy(&(ev->event.data.vsp_cmd.sub_data),292&(vsp_cmd->sub_data), sizeof(vsp_cmd->sub_data));293mb();294295rc = signal_event(ev);296if (rc == 0)297wait_for_completion(&response.com);298return rc;299}300301302/*303* Send a 12-byte CE message to the primary partition VSP object304*/305static int signal_ce_msg(char *ce_msg, struct ce_msg_comp_data *completion)306{307struct pending_event *ev = new_pending_event();308309if (ev == NULL)310return -ENOMEM;311312ev->event.hp_lp_event.xSubtype = 0;313ev->event.hp_lp_event.x.xSubtypeData =314subtype_data('M', 'F', 'C', 'E');315memcpy(ev->event.data.ce_msg.ce_msg, ce_msg, 12);316ev->event.data.ce_msg.completion = completion;317return signal_event(ev);318}319320/*321* Send a 12-byte CE message (with no data) to the primary partition VSP object322*/323static int signal_ce_msg_simple(u8 ce_op, struct ce_msg_comp_data *completion)324{325u8 ce_msg[12];326327memset(ce_msg, 0, sizeof(ce_msg));328ce_msg[3] = ce_op;329return signal_ce_msg(ce_msg, completion);330}331332/*333* Send a 12-byte CE message and DMA data to the primary partition VSP object334*/335static int dma_and_signal_ce_msg(char *ce_msg,336struct ce_msg_comp_data *completion, void *dma_data,337unsigned dma_data_length, unsigned remote_address)338{339struct pending_event *ev = new_pending_event();340341if (ev == NULL)342return -ENOMEM;343344ev->event.hp_lp_event.xSubtype = 0;345ev->event.hp_lp_event.x.xSubtypeData =346subtype_data('M', 'F', 'C', 'E');347memcpy(ev->event.data.ce_msg.ce_msg, ce_msg, 12);348ev->event.data.ce_msg.completion = completion;349memcpy(ev->dma_data, dma_data, dma_data_length);350ev->dma_data_length = dma_data_length;351ev->remote_address = remote_address;352return signal_event(ev);353}354355/*356* Initiate a nice (hopefully) shutdown of Linux. We simply are357* going to try and send the init process a SIGINT signal. If358* this fails (why?), we'll simply force it off in a not-so-nice359* manner.360*/361static int shutdown(void)362{363int rc = kill_cad_pid(SIGINT, 1);364365if (rc) {366printk(KERN_ALERT "mf.c: SIGINT to init failed (%d), "367"hard shutdown commencing\n", rc);368mf_power_off();369} else370printk(KERN_INFO "mf.c: init has been successfully notified "371"to proceed with shutdown\n");372return rc;373}374375/*376* The primary partition VSP object is sending us a new377* event flow. Handle it...378*/379static void handle_int(struct io_mf_lp_event *event)380{381struct ce_msg_data *ce_msg_data;382struct ce_msg_data *pce_msg_data;383unsigned long flags;384struct pending_event *pev;385386/* ack the interrupt */387event->hp_lp_event.xRc = HvLpEvent_Rc_Good;388HvCallEvent_ackLpEvent(&event->hp_lp_event);389390/* process interrupt */391switch (event->hp_lp_event.xSubtype) {392case 0: /* CE message */393ce_msg_data = &event->data.ce_msg;394switch (ce_msg_data->ce_msg[3]) {395case 0x5B: /* power control notification */396if ((ce_msg_data->ce_msg[5] & 0x20) != 0) {397printk(KERN_INFO "mf.c: Commencing partition shutdown\n");398if (shutdown() == 0)399signal_ce_msg_simple(0xDB, NULL);400}401break;402case 0xC0: /* get time */403spin_lock_irqsave(&pending_event_spinlock, flags);404pev = pending_event_head;405if (pev != NULL)406pending_event_head = pending_event_head->next;407spin_unlock_irqrestore(&pending_event_spinlock, flags);408if (pev == NULL)409break;410pce_msg_data = &pev->event.data.ce_msg;411if (pce_msg_data->ce_msg[3] != 0x40)412break;413if (pce_msg_data->completion != NULL) {414ce_msg_comp_hdlr handler =415pce_msg_data->completion->handler;416void *token = pce_msg_data->completion->token;417418if (handler != NULL)419(*handler)(token, ce_msg_data);420}421spin_lock_irqsave(&pending_event_spinlock, flags);422free_pending_event(pev);423spin_unlock_irqrestore(&pending_event_spinlock, flags);424/* send next waiting event */425if (pending_event_head != NULL)426signal_event(NULL);427break;428}429break;430case 1: /* IT sys shutdown */431printk(KERN_INFO "mf.c: Commencing system shutdown\n");432shutdown();433break;434}435}436437/*438* The primary partition VSP object is acknowledging the receipt439* of a flow we sent to them. If there are other flows queued440* up, we must send another one now...441*/442static void handle_ack(struct io_mf_lp_event *event)443{444unsigned long flags;445struct pending_event *two = NULL;446unsigned long free_it = 0;447struct ce_msg_data *ce_msg_data;448struct ce_msg_data *pce_msg_data;449struct vsp_rsp_data *rsp;450451/* handle current event */452if (pending_event_head == NULL) {453printk(KERN_ERR "mf.c: stack empty for receiving ack\n");454return;455}456457switch (event->hp_lp_event.xSubtype) {458case 0: /* CE msg */459ce_msg_data = &event->data.ce_msg;460if (ce_msg_data->ce_msg[3] != 0x40) {461free_it = 1;462break;463}464if (ce_msg_data->ce_msg[2] == 0)465break;466free_it = 1;467pce_msg_data = &pending_event_head->event.data.ce_msg;468if (pce_msg_data->completion != NULL) {469ce_msg_comp_hdlr handler =470pce_msg_data->completion->handler;471void *token = pce_msg_data->completion->token;472473if (handler != NULL)474(*handler)(token, ce_msg_data);475}476break;477case 4: /* allocate */478case 5: /* deallocate */479if (pending_event_head->hdlr != NULL)480(*pending_event_head->hdlr)((void *)event->hp_lp_event.xCorrelationToken, event->data.alloc.count);481free_it = 1;482break;483case 6:484free_it = 1;485rsp = (struct vsp_rsp_data *)event->data.vsp_cmd.token;486if (rsp == NULL) {487printk(KERN_ERR "mf.c: no rsp\n");488break;489}490if (rsp->response != NULL)491memcpy(rsp->response, &event->data.vsp_cmd,492sizeof(event->data.vsp_cmd));493complete(&rsp->com);494break;495}496497/* remove from queue */498spin_lock_irqsave(&pending_event_spinlock, flags);499if ((pending_event_head != NULL) && (free_it == 1)) {500struct pending_event *oldHead = pending_event_head;501502pending_event_head = pending_event_head->next;503two = pending_event_head;504free_pending_event(oldHead);505}506spin_unlock_irqrestore(&pending_event_spinlock, flags);507508/* send next waiting event */509if (two != NULL)510signal_event(NULL);511}512513/*514* This is the generic event handler we are registering with515* the Hypervisor. Ensure the flows are for us, and then516* parse it enough to know if it is an interrupt or an517* acknowledge.518*/519static void hv_handler(struct HvLpEvent *event)520{521if ((event != NULL) && (event->xType == HvLpEvent_Type_MachineFac)) {522if (hvlpevent_is_ack(event))523handle_ack((struct io_mf_lp_event *)event);524else525handle_int((struct io_mf_lp_event *)event);526} else527printk(KERN_ERR "mf.c: alien event received\n");528}529530/*531* Global kernel interface to allocate and seed events into the532* Hypervisor.533*/534void mf_allocate_lp_events(HvLpIndex target_lp, HvLpEvent_Type type,535unsigned size, unsigned count, MFCompleteHandler hdlr,536void *user_token)537{538struct pending_event *ev = new_pending_event();539int rc;540541if (ev == NULL) {542rc = -ENOMEM;543} else {544ev->event.hp_lp_event.xSubtype = 4;545ev->event.hp_lp_event.xCorrelationToken = (u64)user_token;546ev->event.hp_lp_event.x.xSubtypeData =547subtype_data('M', 'F', 'M', 'A');548ev->event.data.alloc.target_lp = target_lp;549ev->event.data.alloc.type = type;550ev->event.data.alloc.size = size;551ev->event.data.alloc.count = count;552ev->hdlr = hdlr;553rc = signal_event(ev);554}555if ((rc != 0) && (hdlr != NULL))556(*hdlr)(user_token, rc);557}558EXPORT_SYMBOL(mf_allocate_lp_events);559560/*561* Global kernel interface to unseed and deallocate events already in562* Hypervisor.563*/564void mf_deallocate_lp_events(HvLpIndex target_lp, HvLpEvent_Type type,565unsigned count, MFCompleteHandler hdlr, void *user_token)566{567struct pending_event *ev = new_pending_event();568int rc;569570if (ev == NULL)571rc = -ENOMEM;572else {573ev->event.hp_lp_event.xSubtype = 5;574ev->event.hp_lp_event.xCorrelationToken = (u64)user_token;575ev->event.hp_lp_event.x.xSubtypeData =576subtype_data('M', 'F', 'M', 'D');577ev->event.data.alloc.target_lp = target_lp;578ev->event.data.alloc.type = type;579ev->event.data.alloc.count = count;580ev->hdlr = hdlr;581rc = signal_event(ev);582}583if ((rc != 0) && (hdlr != NULL))584(*hdlr)(user_token, rc);585}586EXPORT_SYMBOL(mf_deallocate_lp_events);587588/*589* Global kernel interface to tell the VSP object in the primary590* partition to power this partition off.591*/592void mf_power_off(void)593{594printk(KERN_INFO "mf.c: Down it goes...\n");595signal_ce_msg_simple(0x4d, NULL);596for (;;)597;598}599600/*601* Global kernel interface to tell the VSP object in the primary602* partition to reboot this partition.603*/604void mf_reboot(char *cmd)605{606printk(KERN_INFO "mf.c: Preparing to bounce...\n");607signal_ce_msg_simple(0x4e, NULL);608for (;;)609;610}611612/*613* Display a single word SRC onto the VSP control panel.614*/615void mf_display_src(u32 word)616{617u8 ce[12];618619memset(ce, 0, sizeof(ce));620ce[3] = 0x4a;621ce[7] = 0x01;622ce[8] = word >> 24;623ce[9] = word >> 16;624ce[10] = word >> 8;625ce[11] = word;626signal_ce_msg(ce, NULL);627}628629/*630* Display a single word SRC of the form "PROGXXXX" on the VSP control panel.631*/632static __init void mf_display_progress_src(u16 value)633{634u8 ce[12];635u8 src[72];636637memcpy(ce, "\x00\x00\x04\x4A\x00\x00\x00\x48\x00\x00\x00\x00", 12);638memcpy(src, "\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"639"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"640"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"641"\x00\x00\x00\x00PROGxxxx ",64272);643src[6] = value >> 8;644src[7] = value & 255;645src[44] = "0123456789ABCDEF"[(value >> 12) & 15];646src[45] = "0123456789ABCDEF"[(value >> 8) & 15];647src[46] = "0123456789ABCDEF"[(value >> 4) & 15];648src[47] = "0123456789ABCDEF"[value & 15];649dma_and_signal_ce_msg(ce, NULL, src, sizeof(src), 9 * 64 * 1024);650}651652/*653* Clear the VSP control panel. Used to "erase" an SRC that was654* previously displayed.655*/656static void mf_clear_src(void)657{658signal_ce_msg_simple(0x4b, NULL);659}660661void __init mf_display_progress(u16 value)662{663if (!mf_initialized)664return;665666if (0xFFFF == value)667mf_clear_src();668else669mf_display_progress_src(value);670}671672/*673* Initialization code here.674*/675void __init mf_init(void)676{677int i;678679spin_lock_init(&pending_event_spinlock);680681for (i = 0; i < PENDING_EVENT_PREALLOC_LEN; i++)682free_pending_event(&pending_event_prealloc[i]);683684HvLpEvent_registerHandler(HvLpEvent_Type_MachineFac, &hv_handler);685686/* virtual continue ack */687signal_ce_msg_simple(0x57, NULL);688689mf_initialized = 1;690mb();691692printk(KERN_NOTICE "mf.c: iSeries Linux LPAR Machine Facilities "693"initialized\n");694}695696struct rtc_time_data {697struct completion com;698struct ce_msg_data ce_msg;699int rc;700};701702static void get_rtc_time_complete(void *token, struct ce_msg_data *ce_msg)703{704struct rtc_time_data *rtc = token;705706memcpy(&rtc->ce_msg, ce_msg, sizeof(rtc->ce_msg));707rtc->rc = 0;708complete(&rtc->com);709}710711static int mf_set_rtc(struct rtc_time *tm)712{713char ce_time[12];714u8 day, mon, hour, min, sec, y1, y2;715unsigned year;716717year = 1900 + tm->tm_year;718y1 = year / 100;719y2 = year % 100;720721sec = tm->tm_sec;722min = tm->tm_min;723hour = tm->tm_hour;724day = tm->tm_mday;725mon = tm->tm_mon + 1;726727sec = bin2bcd(sec);728min = bin2bcd(min);729hour = bin2bcd(hour);730mon = bin2bcd(mon);731day = bin2bcd(day);732y1 = bin2bcd(y1);733y2 = bin2bcd(y2);734735memset(ce_time, 0, sizeof(ce_time));736ce_time[3] = 0x41;737ce_time[4] = y1;738ce_time[5] = y2;739ce_time[6] = sec;740ce_time[7] = min;741ce_time[8] = hour;742ce_time[10] = day;743ce_time[11] = mon;744745return signal_ce_msg(ce_time, NULL);746}747748static int rtc_set_tm(int rc, u8 *ce_msg, struct rtc_time *tm)749{750tm->tm_wday = 0;751tm->tm_yday = 0;752tm->tm_isdst = 0;753if (rc) {754tm->tm_sec = 0;755tm->tm_min = 0;756tm->tm_hour = 0;757tm->tm_mday = 15;758tm->tm_mon = 5;759tm->tm_year = 52;760return rc;761}762763if ((ce_msg[2] == 0xa9) ||764(ce_msg[2] == 0xaf)) {765/* TOD clock is not set */766tm->tm_sec = 1;767tm->tm_min = 1;768tm->tm_hour = 1;769tm->tm_mday = 10;770tm->tm_mon = 8;771tm->tm_year = 71;772mf_set_rtc(tm);773}774{775u8 year = ce_msg[5];776u8 sec = ce_msg[6];777u8 min = ce_msg[7];778u8 hour = ce_msg[8];779u8 day = ce_msg[10];780u8 mon = ce_msg[11];781782sec = bcd2bin(sec);783min = bcd2bin(min);784hour = bcd2bin(hour);785day = bcd2bin(day);786mon = bcd2bin(mon);787year = bcd2bin(year);788789if (year <= 69)790year += 100;791792tm->tm_sec = sec;793tm->tm_min = min;794tm->tm_hour = hour;795tm->tm_mday = day;796tm->tm_mon = mon;797tm->tm_year = year;798}799800return 0;801}802803static int mf_get_rtc(struct rtc_time *tm)804{805struct ce_msg_comp_data ce_complete;806struct rtc_time_data rtc_data;807int rc;808809memset(&ce_complete, 0, sizeof(ce_complete));810memset(&rtc_data, 0, sizeof(rtc_data));811init_completion(&rtc_data.com);812ce_complete.handler = &get_rtc_time_complete;813ce_complete.token = &rtc_data;814rc = signal_ce_msg_simple(0x40, &ce_complete);815if (rc)816return rc;817wait_for_completion(&rtc_data.com);818return rtc_set_tm(rtc_data.rc, rtc_data.ce_msg.ce_msg, tm);819}820821struct boot_rtc_time_data {822int busy;823struct ce_msg_data ce_msg;824int rc;825};826827static void get_boot_rtc_time_complete(void *token, struct ce_msg_data *ce_msg)828{829struct boot_rtc_time_data *rtc = token;830831memcpy(&rtc->ce_msg, ce_msg, sizeof(rtc->ce_msg));832rtc->rc = 0;833rtc->busy = 0;834}835836static int mf_get_boot_rtc(struct rtc_time *tm)837{838struct ce_msg_comp_data ce_complete;839struct boot_rtc_time_data rtc_data;840int rc;841842memset(&ce_complete, 0, sizeof(ce_complete));843memset(&rtc_data, 0, sizeof(rtc_data));844rtc_data.busy = 1;845ce_complete.handler = &get_boot_rtc_time_complete;846ce_complete.token = &rtc_data;847rc = signal_ce_msg_simple(0x40, &ce_complete);848if (rc)849return rc;850/* We need to poll here as we are not yet taking interrupts */851while (rtc_data.busy) {852if (hvlpevent_is_pending())853process_hvlpevents();854}855return rtc_set_tm(rtc_data.rc, rtc_data.ce_msg.ce_msg, tm);856}857858#ifdef CONFIG_PROC_FS859static int mf_cmdline_proc_show(struct seq_file *m, void *v)860{861char *page, *p;862struct vsp_cmd_data vsp_cmd;863int rc;864dma_addr_t dma_addr;865866/* The HV appears to return no more than 256 bytes of command line */867page = kmalloc(256, GFP_KERNEL);868if (!page)869return -ENOMEM;870871dma_addr = iseries_hv_map(page, 256, DMA_FROM_DEVICE);872if (dma_addr == DMA_ERROR_CODE) {873kfree(page);874return -ENOMEM;875}876memset(page, 0, 256);877memset(&vsp_cmd, 0, sizeof(vsp_cmd));878vsp_cmd.cmd = 33;879vsp_cmd.sub_data.kern.token = dma_addr;880vsp_cmd.sub_data.kern.address_type = HvLpDma_AddressType_TceIndex;881vsp_cmd.sub_data.kern.side = (u64)m->private;882vsp_cmd.sub_data.kern.length = 256;883mb();884rc = signal_vsp_instruction(&vsp_cmd);885iseries_hv_unmap(dma_addr, 256, DMA_FROM_DEVICE);886if (rc) {887kfree(page);888return rc;889}890if (vsp_cmd.result_code != 0) {891kfree(page);892return -ENOMEM;893}894p = page;895while (p - page < 256) {896if (*p == '\0' || *p == '\n') {897*p = '\n';898break;899}900p++;901902}903seq_write(m, page, p - page);904kfree(page);905return 0;906}907908static int mf_cmdline_proc_open(struct inode *inode, struct file *file)909{910return single_open(file, mf_cmdline_proc_show, PDE(inode)->data);911}912913#if 0914static int mf_getVmlinuxChunk(char *buffer, int *size, int offset, u64 side)915{916struct vsp_cmd_data vsp_cmd;917int rc;918int len = *size;919dma_addr_t dma_addr;920921dma_addr = iseries_hv_map(buffer, len, DMA_FROM_DEVICE);922memset(buffer, 0, len);923memset(&vsp_cmd, 0, sizeof(vsp_cmd));924vsp_cmd.cmd = 32;925vsp_cmd.sub_data.kern.token = dma_addr;926vsp_cmd.sub_data.kern.address_type = HvLpDma_AddressType_TceIndex;927vsp_cmd.sub_data.kern.side = side;928vsp_cmd.sub_data.kern.offset = offset;929vsp_cmd.sub_data.kern.length = len;930mb();931rc = signal_vsp_instruction(&vsp_cmd);932if (rc == 0) {933if (vsp_cmd.result_code == 0)934*size = vsp_cmd.sub_data.length_out;935else936rc = -ENOMEM;937}938939iseries_hv_unmap(dma_addr, len, DMA_FROM_DEVICE);940941return rc;942}943944static int proc_mf_dump_vmlinux(char *page, char **start, off_t off,945int count, int *eof, void *data)946{947int sizeToGet = count;948949if (!capable(CAP_SYS_ADMIN))950return -EACCES;951952if (mf_getVmlinuxChunk(page, &sizeToGet, off, (u64)data) == 0) {953if (sizeToGet != 0) {954*start = page + off;955return sizeToGet;956}957*eof = 1;958return 0;959}960*eof = 1;961return 0;962}963#endif964965static int mf_side_proc_show(struct seq_file *m, void *v)966{967char mf_current_side = ' ';968struct vsp_cmd_data vsp_cmd;969970memset(&vsp_cmd, 0, sizeof(vsp_cmd));971vsp_cmd.cmd = 2;972vsp_cmd.sub_data.ipl_type = 0;973mb();974975if (signal_vsp_instruction(&vsp_cmd) == 0) {976if (vsp_cmd.result_code == 0) {977switch (vsp_cmd.sub_data.ipl_type) {978case 0: mf_current_side = 'A';979break;980case 1: mf_current_side = 'B';981break;982case 2: mf_current_side = 'C';983break;984default: mf_current_side = 'D';985break;986}987}988}989990seq_printf(m, "%c\n", mf_current_side);991return 0;992}993994static int mf_side_proc_open(struct inode *inode, struct file *file)995{996return single_open(file, mf_side_proc_show, NULL);997}998999static ssize_t mf_side_proc_write(struct file *file, const char __user *buffer,1000size_t count, loff_t *pos)1001{1002char side;1003u64 newSide;1004struct vsp_cmd_data vsp_cmd;10051006if (!capable(CAP_SYS_ADMIN))1007return -EACCES;10081009if (count == 0)1010return 0;10111012if (get_user(side, buffer))1013return -EFAULT;10141015switch (side) {1016case 'A': newSide = 0;1017break;1018case 'B': newSide = 1;1019break;1020case 'C': newSide = 2;1021break;1022case 'D': newSide = 3;1023break;1024default:1025printk(KERN_ERR "mf_proc.c: proc_mf_change_side: invalid side\n");1026return -EINVAL;1027}10281029memset(&vsp_cmd, 0, sizeof(vsp_cmd));1030vsp_cmd.sub_data.ipl_type = newSide;1031vsp_cmd.cmd = 10;10321033(void)signal_vsp_instruction(&vsp_cmd);10341035return count;1036}10371038static const struct file_operations mf_side_proc_fops = {1039.owner = THIS_MODULE,1040.open = mf_side_proc_open,1041.read = seq_read,1042.llseek = seq_lseek,1043.release = single_release,1044.write = mf_side_proc_write,1045};10461047static int mf_src_proc_show(struct seq_file *m, void *v)1048{1049return 0;1050}10511052static int mf_src_proc_open(struct inode *inode, struct file *file)1053{1054return single_open(file, mf_src_proc_show, NULL);1055}10561057static ssize_t mf_src_proc_write(struct file *file, const char __user *buffer,1058size_t count, loff_t *pos)1059{1060char stkbuf[10];10611062if (!capable(CAP_SYS_ADMIN))1063return -EACCES;10641065if ((count < 4) && (count != 1)) {1066printk(KERN_ERR "mf_proc: invalid src\n");1067return -EINVAL;1068}10691070if (count > (sizeof(stkbuf) - 1))1071count = sizeof(stkbuf) - 1;1072if (copy_from_user(stkbuf, buffer, count))1073return -EFAULT;10741075if ((count == 1) && (*stkbuf == '\0'))1076mf_clear_src();1077else1078mf_display_src(*(u32 *)stkbuf);10791080return count;1081}10821083static const struct file_operations mf_src_proc_fops = {1084.owner = THIS_MODULE,1085.open = mf_src_proc_open,1086.read = seq_read,1087.llseek = seq_lseek,1088.release = single_release,1089.write = mf_src_proc_write,1090};10911092static ssize_t mf_cmdline_proc_write(struct file *file, const char __user *buffer,1093size_t count, loff_t *pos)1094{1095void *data = PDE(file->f_path.dentry->d_inode)->data;1096struct vsp_cmd_data vsp_cmd;1097dma_addr_t dma_addr;1098char *page;1099int ret = -EACCES;11001101if (!capable(CAP_SYS_ADMIN))1102goto out;11031104dma_addr = 0;1105page = iseries_hv_alloc(count, &dma_addr, GFP_ATOMIC);1106ret = -ENOMEM;1107if (page == NULL)1108goto out;11091110ret = -EFAULT;1111if (copy_from_user(page, buffer, count))1112goto out_free;11131114memset(&vsp_cmd, 0, sizeof(vsp_cmd));1115vsp_cmd.cmd = 31;1116vsp_cmd.sub_data.kern.token = dma_addr;1117vsp_cmd.sub_data.kern.address_type = HvLpDma_AddressType_TceIndex;1118vsp_cmd.sub_data.kern.side = (u64)data;1119vsp_cmd.sub_data.kern.length = count;1120mb();1121(void)signal_vsp_instruction(&vsp_cmd);1122ret = count;11231124out_free:1125iseries_hv_free(count, page, dma_addr);1126out:1127return ret;1128}11291130static const struct file_operations mf_cmdline_proc_fops = {1131.owner = THIS_MODULE,1132.open = mf_cmdline_proc_open,1133.read = seq_read,1134.llseek = seq_lseek,1135.release = single_release,1136.write = mf_cmdline_proc_write,1137};11381139static ssize_t proc_mf_change_vmlinux(struct file *file,1140const char __user *buf,1141size_t count, loff_t *ppos)1142{1143struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);1144ssize_t rc;1145dma_addr_t dma_addr;1146char *page;1147struct vsp_cmd_data vsp_cmd;11481149rc = -EACCES;1150if (!capable(CAP_SYS_ADMIN))1151goto out;11521153dma_addr = 0;1154page = iseries_hv_alloc(count, &dma_addr, GFP_ATOMIC);1155rc = -ENOMEM;1156if (page == NULL) {1157printk(KERN_ERR "mf.c: couldn't allocate memory to set vmlinux chunk\n");1158goto out;1159}1160rc = -EFAULT;1161if (copy_from_user(page, buf, count))1162goto out_free;11631164memset(&vsp_cmd, 0, sizeof(vsp_cmd));1165vsp_cmd.cmd = 30;1166vsp_cmd.sub_data.kern.token = dma_addr;1167vsp_cmd.sub_data.kern.address_type = HvLpDma_AddressType_TceIndex;1168vsp_cmd.sub_data.kern.side = (u64)dp->data;1169vsp_cmd.sub_data.kern.offset = *ppos;1170vsp_cmd.sub_data.kern.length = count;1171mb();1172rc = signal_vsp_instruction(&vsp_cmd);1173if (rc)1174goto out_free;1175rc = -ENOMEM;1176if (vsp_cmd.result_code != 0)1177goto out_free;11781179*ppos += count;1180rc = count;1181out_free:1182iseries_hv_free(count, page, dma_addr);1183out:1184return rc;1185}11861187static const struct file_operations proc_vmlinux_operations = {1188.write = proc_mf_change_vmlinux,1189.llseek = default_llseek,1190};11911192static int __init mf_proc_init(void)1193{1194struct proc_dir_entry *mf_proc_root;1195struct proc_dir_entry *ent;1196struct proc_dir_entry *mf;1197char name[2];1198int i;11991200if (!firmware_has_feature(FW_FEATURE_ISERIES))1201return 0;12021203mf_proc_root = proc_mkdir("iSeries/mf", NULL);1204if (!mf_proc_root)1205return 1;12061207name[1] = '\0';1208for (i = 0; i < 4; i++) {1209name[0] = 'A' + i;1210mf = proc_mkdir(name, mf_proc_root);1211if (!mf)1212return 1;12131214ent = proc_create_data("cmdline", S_IRUSR|S_IWUSR, mf,1215&mf_cmdline_proc_fops, (void *)(long)i);1216if (!ent)1217return 1;12181219if (i == 3) /* no vmlinux entry for 'D' */1220continue;12211222ent = proc_create_data("vmlinux", S_IFREG|S_IWUSR, mf,1223&proc_vmlinux_operations,1224(void *)(long)i);1225if (!ent)1226return 1;1227}12281229ent = proc_create("side", S_IFREG|S_IRUSR|S_IWUSR, mf_proc_root,1230&mf_side_proc_fops);1231if (!ent)1232return 1;12331234ent = proc_create("src", S_IFREG|S_IRUSR|S_IWUSR, mf_proc_root,1235&mf_src_proc_fops);1236if (!ent)1237return 1;12381239return 0;1240}12411242__initcall(mf_proc_init);12431244#endif /* CONFIG_PROC_FS */12451246/*1247* Get the RTC from the virtual service processor1248* This requires flowing LpEvents to the primary partition1249*/1250void iSeries_get_rtc_time(struct rtc_time *rtc_tm)1251{1252mf_get_rtc(rtc_tm);1253rtc_tm->tm_mon--;1254}12551256/*1257* Set the RTC in the virtual service processor1258* This requires flowing LpEvents to the primary partition1259*/1260int iSeries_set_rtc_time(struct rtc_time *tm)1261{1262mf_set_rtc(tm);1263return 0;1264}12651266unsigned long iSeries_get_boot_time(void)1267{1268struct rtc_time tm;12691270mf_get_boot_rtc(&tm);1271return mktime(tm.tm_year + 1900, tm.tm_mon, tm.tm_mday,1272tm.tm_hour, tm.tm_min, tm.tm_sec);1273}127412751276