linux/arch/powerpc/platforms/iseries/mf.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2001 Troy D. Armstrong  IBM Corporation
   3 * Copyright (C) 2004-2005 Stephen Rothwell  IBM Corporation
   4 *
   5 * This modules exists as an interface between a Linux secondary partition
   6 * running on an iSeries and the primary partition's Virtual Service
   7 * Processor (VSP) object.  The VSP has final authority over powering on/off
   8 * all partitions in the iSeries.  It also provides miscellaneous low-level
   9 * machine facility type operations.
  10 *
  11 *
  12 * This program is free software; you can redistribute it and/or modify
  13 * it under the terms of the GNU General Public License as published by
  14 * the Free Software Foundation; either version 2 of the License, or
  15 * (at your option) any later version.
  16 *
  17 * This program is distributed in the hope that it will be useful,
  18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20 * GNU General Public License for more details.
  21 *
  22 * You should have received a copy of the GNU General Public License
  23 * along with this program; if not, write to the Free Software
  24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  25 */
  26
  27#include <linux/types.h>
  28#include <linux/errno.h>
  29#include <linux/kernel.h>
  30#include <linux/init.h>
  31#include <linux/completion.h>
  32#include <linux/delay.h>
  33#include <linux/dma-mapping.h>
  34#include <linux/bcd.h>
  35#include <linux/rtc.h>
  36
  37#include <asm/time.h>
  38#include <asm/uaccess.h>
  39#include <asm/paca.h>
  40#include <asm/abs_addr.h>
  41#include <asm/firmware.h>
  42#include <asm/iseries/mf.h>
  43#include <asm/iseries/hv_lp_config.h>
  44#include <asm/iseries/hv_lp_event.h>
  45#include <asm/iseries/it_lp_queue.h>
  46
  47#include "setup.h"
  48
  49static int mf_initialized;
  50
  51/*
  52 * This is the structure layout for the Machine Facilites LPAR event
  53 * flows.
  54 */
  55struct vsp_cmd_data {
  56        u64 token;
  57        u16 cmd;
  58        HvLpIndex lp_index;
  59        u8 result_code;
  60        u32 reserved;
  61        union {
  62                u64 state;      /* GetStateOut */
  63                u64 ipl_type;   /* GetIplTypeOut, Function02SelectIplTypeIn */
  64                u64 ipl_mode;   /* GetIplModeOut, Function02SelectIplModeIn */
  65                u64 page[4];    /* GetSrcHistoryIn */
  66                u64 flag;       /* GetAutoIplWhenPrimaryIplsOut,
  67                                   SetAutoIplWhenPrimaryIplsIn,
  68                                   WhiteButtonPowerOffIn,
  69                                   Function08FastPowerOffIn,
  70                                   IsSpcnRackPowerIncompleteOut */
  71                struct {
  72                        u64 token;
  73                        u64 address_type;
  74                        u64 side;
  75                        u32 length;
  76                        u32 offset;
  77                } kern;         /* SetKernelImageIn, GetKernelImageIn,
  78                                   SetKernelCmdLineIn, GetKernelCmdLineIn */
  79                u32 length_out; /* GetKernelImageOut, GetKernelCmdLineOut */
  80                u8 reserved[80];
  81        } sub_data;
  82};
  83
  84struct vsp_rsp_data {
  85        struct completion com;
  86        struct vsp_cmd_data *response;
  87};
  88
  89struct alloc_data {
  90        u16 size;
  91        u16 type;
  92        u32 count;
  93        u16 reserved1;
  94        u8 reserved2;
  95        HvLpIndex target_lp;
  96};
  97
  98struct ce_msg_data;
  99
 100typedef void (*ce_msg_comp_hdlr)(void *token, struct ce_msg_data *vsp_cmd_rsp);
 101
 102struct ce_msg_comp_data {
 103        ce_msg_comp_hdlr handler;
 104        void *token;
 105};
 106
 107struct ce_msg_data {
 108        u8 ce_msg[12];
 109        char reserved[4];
 110        struct ce_msg_comp_data *completion;
 111};
 112
 113struct io_mf_lp_event {
 114        struct HvLpEvent hp_lp_event;
 115        u16 subtype_result_code;
 116        u16 reserved1;
 117        u32 reserved2;
 118        union {
 119                struct alloc_data alloc;
 120                struct ce_msg_data ce_msg;
 121                struct vsp_cmd_data vsp_cmd;
 122        } data;
 123};
 124
 125#define subtype_data(a, b, c, d)        \
 126                (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
 127
 128/*
 129 * All outgoing event traffic is kept on a FIFO queue.  The first
 130 * pointer points to the one that is outstanding, and all new
 131 * requests get stuck on the end.  Also, we keep a certain number of
 132 * preallocated pending events so that we can operate very early in
 133 * the boot up sequence (before kmalloc is ready).
 134 */
 135struct pending_event {
 136        struct pending_event *next;
 137        struct io_mf_lp_event event;
 138        MFCompleteHandler hdlr;
 139        char dma_data[72];
 140        unsigned dma_data_length;
 141        unsigned remote_address;
 142};
 143static spinlock_t pending_event_spinlock;
 144static struct pending_event *pending_event_head;
 145static struct pending_event *pending_event_tail;
 146static struct pending_event *pending_event_avail;
 147#define PENDING_EVENT_PREALLOC_LEN 16
 148static struct pending_event pending_event_prealloc[PENDING_EVENT_PREALLOC_LEN];
 149
 150/*
 151 * Put a pending event onto the available queue, so it can get reused.
 152 * Attention! You must have the pending_event_spinlock before calling!
 153 */
 154static void free_pending_event(struct pending_event *ev)
 155{
 156        if (ev != NULL) {
 157                ev->next = pending_event_avail;
 158                pending_event_avail = ev;
 159        }
 160}
 161
 162/*
 163 * Enqueue the outbound event onto the stack.  If the queue was
 164 * empty to begin with, we must also issue it via the Hypervisor
 165 * interface.  There is a section of code below that will touch
 166 * the first stack pointer without the protection of the pending_event_spinlock.
 167 * This is OK, because we know that nobody else will be modifying
 168 * the first pointer when we do this.
 169 */
 170static int signal_event(struct pending_event *ev)
 171{
 172        int rc = 0;
 173        unsigned long flags;
 174        int go = 1;
 175        struct pending_event *ev1;
 176        HvLpEvent_Rc hv_rc;
 177
 178        /* enqueue the event */
 179        if (ev != NULL) {
 180                ev->next = NULL;
 181                spin_lock_irqsave(&pending_event_spinlock, flags);
 182                if (pending_event_head == NULL)
 183                        pending_event_head = ev;
 184                else {
 185                        go = 0;
 186                        pending_event_tail->next = ev;
 187                }
 188                pending_event_tail = ev;
 189                spin_unlock_irqrestore(&pending_event_spinlock, flags);
 190        }
 191
 192        /* send the event */
 193        while (go) {
 194                go = 0;
 195
 196                /* any DMA data to send beforehand? */
 197                if (pending_event_head->dma_data_length > 0)
 198                        HvCallEvent_dmaToSp(pending_event_head->dma_data,
 199                                        pending_event_head->remote_address,
 200                                        pending_event_head->dma_data_length,
 201                                        HvLpDma_Direction_LocalToRemote);
 202
 203                hv_rc = HvCallEvent_signalLpEvent(
 204                                &pending_event_head->event.hp_lp_event);
 205                if (hv_rc != HvLpEvent_Rc_Good) {
 206                        printk(KERN_ERR "mf.c: HvCallEvent_signalLpEvent() "
 207                                        "failed with %d\n", (int)hv_rc);
 208
 209                        spin_lock_irqsave(&pending_event_spinlock, flags);
 210                        ev1 = pending_event_head;
 211                        pending_event_head = pending_event_head->next;
 212                        if (pending_event_head != NULL)
 213                                go = 1;
 214                        spin_unlock_irqrestore(&pending_event_spinlock, flags);
 215
 216                        if (ev1 == ev)
 217                                rc = -EIO;
 218                        else if (ev1->hdlr != NULL)
 219                                (*ev1->hdlr)((void *)ev1->event.hp_lp_event.xCorrelationToken, -EIO);
 220
 221                        spin_lock_irqsave(&pending_event_spinlock, flags);
 222                        free_pending_event(ev1);
 223                        spin_unlock_irqrestore(&pending_event_spinlock, flags);
 224                }
 225        }
 226
 227        return rc;
 228}
 229
 230/*
 231 * Allocate a new pending_event structure, and initialize it.
 232 */
 233static struct pending_event *new_pending_event(void)
 234{
 235        struct pending_event *ev = NULL;
 236        HvLpIndex primary_lp = HvLpConfig_getPrimaryLpIndex();
 237        unsigned long flags;
 238        struct HvLpEvent *hev;
 239
 240        spin_lock_irqsave(&pending_event_spinlock, flags);
 241        if (pending_event_avail != NULL) {
 242                ev = pending_event_avail;
 243                pending_event_avail = pending_event_avail->next;
 244        }
 245        spin_unlock_irqrestore(&pending_event_spinlock, flags);
 246        if (ev == NULL) {
 247                ev = kmalloc(sizeof(struct pending_event), GFP_ATOMIC);
 248                if (ev == NULL) {
 249                        printk(KERN_ERR "mf.c: unable to kmalloc %ld bytes\n",
 250                                        sizeof(struct pending_event));
 251                        return NULL;
 252                }
 253        }
 254        memset(ev, 0, sizeof(struct pending_event));
 255        hev = &ev->event.hp_lp_event;
 256        hev->flags = HV_LP_EVENT_VALID | HV_LP_EVENT_DO_ACK | HV_LP_EVENT_INT;
 257        hev->xType = HvLpEvent_Type_MachineFac;
 258        hev->xSourceLp = HvLpConfig_getLpIndex();
 259        hev->xTargetLp = primary_lp;
 260        hev->xSizeMinus1 = sizeof(ev->event) - 1;
 261        hev->xRc = HvLpEvent_Rc_Good;
 262        hev->xSourceInstanceId = HvCallEvent_getSourceLpInstanceId(primary_lp,
 263                        HvLpEvent_Type_MachineFac);
 264        hev->xTargetInstanceId = HvCallEvent_getTargetLpInstanceId(primary_lp,
 265                        HvLpEvent_Type_MachineFac);
 266
 267        return ev;
 268}
 269
 270static int __maybe_unused
 271signal_vsp_instruction(struct vsp_cmd_data *vsp_cmd)
 272{
 273        struct pending_event *ev = new_pending_event();
 274        int rc;
 275        struct vsp_rsp_data response;
 276
 277        if (ev == NULL)
 278                return -ENOMEM;
 279
 280        init_completion(&response.com);
 281        response.response = vsp_cmd;
 282        ev->event.hp_lp_event.xSubtype = 6;
 283        ev->event.hp_lp_event.x.xSubtypeData =
 284                subtype_data('M', 'F',  'V',  'I');
 285        ev->event.data.vsp_cmd.token = (u64)&response;
 286        ev->event.data.vsp_cmd.cmd = vsp_cmd->cmd;
 287        ev->event.data.vsp_cmd.lp_index = HvLpConfig_getLpIndex();
 288        ev->event.data.vsp_cmd.result_code = 0xFF;
 289        ev->event.data.vsp_cmd.reserved = 0;
 290        memcpy(&(ev->event.data.vsp_cmd.sub_data),
 291                        &(vsp_cmd->sub_data), sizeof(vsp_cmd->sub_data));
 292        mb();
 293
 294        rc = signal_event(ev);
 295        if (rc == 0)
 296                wait_for_completion(&response.com);
 297        return rc;
 298}
 299
 300
 301/*
 302 * Send a 12-byte CE message to the primary partition VSP object
 303 */
 304static int signal_ce_msg(char *ce_msg, struct ce_msg_comp_data *completion)
 305{
 306        struct pending_event *ev = new_pending_event();
 307
 308        if (ev == NULL)
 309                return -ENOMEM;
 310
 311        ev->event.hp_lp_event.xSubtype = 0;
 312        ev->event.hp_lp_event.x.xSubtypeData =
 313                subtype_data('M',  'F',  'C',  'E');
 314        memcpy(ev->event.data.ce_msg.ce_msg, ce_msg, 12);
 315        ev->event.data.ce_msg.completion = completion;
 316        return signal_event(ev);
 317}
 318
 319/*
 320 * Send a 12-byte CE message (with no data) to the primary partition VSP object
 321 */
 322static int signal_ce_msg_simple(u8 ce_op, struct ce_msg_comp_data *completion)
 323{
 324        u8 ce_msg[12];
 325
 326        memset(ce_msg, 0, sizeof(ce_msg));
 327        ce_msg[3] = ce_op;
 328        return signal_ce_msg(ce_msg, completion);
 329}
 330
 331/*
 332 * Send a 12-byte CE message and DMA data to the primary partition VSP object
 333 */
 334static int dma_and_signal_ce_msg(char *ce_msg,
 335                struct ce_msg_comp_data *completion, void *dma_data,
 336                unsigned dma_data_length, unsigned remote_address)
 337{
 338        struct pending_event *ev = new_pending_event();
 339
 340        if (ev == NULL)
 341                return -ENOMEM;
 342
 343        ev->event.hp_lp_event.xSubtype = 0;
 344        ev->event.hp_lp_event.x.xSubtypeData =
 345                subtype_data('M', 'F', 'C', 'E');
 346        memcpy(ev->event.data.ce_msg.ce_msg, ce_msg, 12);
 347        ev->event.data.ce_msg.completion = completion;
 348        memcpy(ev->dma_data, dma_data, dma_data_length);
 349        ev->dma_data_length = dma_data_length;
 350        ev->remote_address = remote_address;
 351        return signal_event(ev);
 352}
 353
 354/*
 355 * Initiate a nice (hopefully) shutdown of Linux.  We simply are
 356 * going to try and send the init process a SIGINT signal.  If
 357 * this fails (why?), we'll simply force it off in a not-so-nice
 358 * manner.
 359 */
 360static int shutdown(void)
 361{
 362        int rc = kill_cad_pid(SIGINT, 1);
 363
 364        if (rc) {
 365                printk(KERN_ALERT "mf.c: SIGINT to init failed (%d), "
 366                                "hard shutdown commencing\n", rc);
 367                mf_power_off();
 368        } else
 369                printk(KERN_INFO "mf.c: init has been successfully notified "
 370                                "to proceed with shutdown\n");
 371        return rc;
 372}
 373
 374/*
 375 * The primary partition VSP object is sending us a new
 376 * event flow.  Handle it...
 377 */
 378static void handle_int(struct io_mf_lp_event *event)
 379{
 380        struct ce_msg_data *ce_msg_data;
 381        struct ce_msg_data *pce_msg_data;
 382        unsigned long flags;
 383        struct pending_event *pev;
 384
 385        /* ack the interrupt */
 386        event->hp_lp_event.xRc = HvLpEvent_Rc_Good;
 387        HvCallEvent_ackLpEvent(&event->hp_lp_event);
 388
 389        /* process interrupt */
 390        switch (event->hp_lp_event.xSubtype) {
 391        case 0: /* CE message */
 392                ce_msg_data = &event->data.ce_msg;
 393                switch (ce_msg_data->ce_msg[3]) {
 394                case 0x5B:      /* power control notification */
 395                        if ((ce_msg_data->ce_msg[5] & 0x20) != 0) {
 396                                printk(KERN_INFO "mf.c: Commencing partition shutdown\n");
 397                                if (shutdown() == 0)
 398                                        signal_ce_msg_simple(0xDB, NULL);
 399                        }
 400                        break;
 401                case 0xC0:      /* get time */
 402                        spin_lock_irqsave(&pending_event_spinlock, flags);
 403                        pev = pending_event_head;
 404                        if (pev != NULL)
 405                                pending_event_head = pending_event_head->next;
 406                        spin_unlock_irqrestore(&pending_event_spinlock, flags);
 407                        if (pev == NULL)
 408                                break;
 409                        pce_msg_data = &pev->event.data.ce_msg;
 410                        if (pce_msg_data->ce_msg[3] != 0x40)
 411                                break;
 412                        if (pce_msg_data->completion != NULL) {
 413                                ce_msg_comp_hdlr handler =
 414                                        pce_msg_data->completion->handler;
 415                                void *token = pce_msg_data->completion->token;
 416
 417                                if (handler != NULL)
 418                                        (*handler)(token, ce_msg_data);
 419                        }
 420                        spin_lock_irqsave(&pending_event_spinlock, flags);
 421                        free_pending_event(pev);
 422                        spin_unlock_irqrestore(&pending_event_spinlock, flags);
 423                        /* send next waiting event */
 424                        if (pending_event_head != NULL)
 425                                signal_event(NULL);
 426                        break;
 427                }
 428                break;
 429        case 1: /* IT sys shutdown */
 430                printk(KERN_INFO "mf.c: Commencing system shutdown\n");
 431                shutdown();
 432                break;
 433        }
 434}
 435
 436/*
 437 * The primary partition VSP object is acknowledging the receipt
 438 * of a flow we sent to them.  If there are other flows queued
 439 * up, we must send another one now...
 440 */
 441static void handle_ack(struct io_mf_lp_event *event)
 442{
 443        unsigned long flags;
 444        struct pending_event *two = NULL;
 445        unsigned long free_it = 0;
 446        struct ce_msg_data *ce_msg_data;
 447        struct ce_msg_data *pce_msg_data;
 448        struct vsp_rsp_data *rsp;
 449
 450        /* handle current event */
 451        if (pending_event_head == NULL) {
 452                printk(KERN_ERR "mf.c: stack empty for receiving ack\n");
 453                return;
 454        }
 455
 456        switch (event->hp_lp_event.xSubtype) {
 457        case 0:     /* CE msg */
 458                ce_msg_data = &event->data.ce_msg;
 459                if (ce_msg_data->ce_msg[3] != 0x40) {
 460                        free_it = 1;
 461                        break;
 462                }
 463                if (ce_msg_data->ce_msg[2] == 0)
 464                        break;
 465                free_it = 1;
 466                pce_msg_data = &pending_event_head->event.data.ce_msg;
 467                if (pce_msg_data->completion != NULL) {
 468                        ce_msg_comp_hdlr handler =
 469                                pce_msg_data->completion->handler;
 470                        void *token = pce_msg_data->completion->token;
 471
 472                        if (handler != NULL)
 473                                (*handler)(token, ce_msg_data);
 474                }
 475                break;
 476        case 4: /* allocate */
 477        case 5: /* deallocate */
 478                if (pending_event_head->hdlr != NULL)
 479                        (*pending_event_head->hdlr)((void *)event->hp_lp_event.xCorrelationToken, event->data.alloc.count);
 480                free_it = 1;
 481                break;
 482        case 6:
 483                free_it = 1;
 484                rsp = (struct vsp_rsp_data *)event->data.vsp_cmd.token;
 485                if (rsp == NULL) {
 486                        printk(KERN_ERR "mf.c: no rsp\n");
 487                        break;
 488                }
 489                if (rsp->response != NULL)
 490                        memcpy(rsp->response, &event->data.vsp_cmd,
 491                                        sizeof(event->data.vsp_cmd));
 492                complete(&rsp->com);
 493                break;
 494        }
 495
 496        /* remove from queue */
 497        spin_lock_irqsave(&pending_event_spinlock, flags);
 498        if ((pending_event_head != NULL) && (free_it == 1)) {
 499                struct pending_event *oldHead = pending_event_head;
 500
 501                pending_event_head = pending_event_head->next;
 502                two = pending_event_head;
 503                free_pending_event(oldHead);
 504        }
 505        spin_unlock_irqrestore(&pending_event_spinlock, flags);
 506
 507        /* send next waiting event */
 508        if (two != NULL)
 509                signal_event(NULL);
 510}
 511
 512/*
 513 * This is the generic event handler we are registering with
 514 * the Hypervisor.  Ensure the flows are for us, and then
 515 * parse it enough to know if it is an interrupt or an
 516 * acknowledge.
 517 */
 518static void hv_handler(struct HvLpEvent *event)
 519{
 520        if ((event != NULL) && (event->xType == HvLpEvent_Type_MachineFac)) {
 521                if (hvlpevent_is_ack(event))
 522                        handle_ack((struct io_mf_lp_event *)event);
 523                else
 524                        handle_int((struct io_mf_lp_event *)event);
 525        } else
 526                printk(KERN_ERR "mf.c: alien event received\n");
 527}
 528
 529/*
 530 * Global kernel interface to allocate and seed events into the
 531 * Hypervisor.
 532 */
 533void mf_allocate_lp_events(HvLpIndex target_lp, HvLpEvent_Type type,
 534                unsigned size, unsigned count, MFCompleteHandler hdlr,
 535                void *user_token)
 536{
 537        struct pending_event *ev = new_pending_event();
 538        int rc;
 539
 540        if (ev == NULL) {
 541                rc = -ENOMEM;
 542        } else {
 543                ev->event.hp_lp_event.xSubtype = 4;
 544                ev->event.hp_lp_event.xCorrelationToken = (u64)user_token;
 545                ev->event.hp_lp_event.x.xSubtypeData =
 546                        subtype_data('M', 'F', 'M', 'A');
 547                ev->event.data.alloc.target_lp = target_lp;
 548                ev->event.data.alloc.type = type;
 549                ev->event.data.alloc.size = size;
 550                ev->event.data.alloc.count = count;
 551                ev->hdlr = hdlr;
 552                rc = signal_event(ev);
 553        }
 554        if ((rc != 0) && (hdlr != NULL))
 555                (*hdlr)(user_token, rc);
 556}
 557EXPORT_SYMBOL(mf_allocate_lp_events);
 558
 559/*
 560 * Global kernel interface to unseed and deallocate events already in
 561 * Hypervisor.
 562 */
 563void mf_deallocate_lp_events(HvLpIndex target_lp, HvLpEvent_Type type,
 564                unsigned count, MFCompleteHandler hdlr, void *user_token)
 565{
 566        struct pending_event *ev = new_pending_event();
 567        int rc;
 568
 569        if (ev == NULL)
 570                rc = -ENOMEM;
 571        else {
 572                ev->event.hp_lp_event.xSubtype = 5;
 573                ev->event.hp_lp_event.xCorrelationToken = (u64)user_token;
 574                ev->event.hp_lp_event.x.xSubtypeData =
 575                        subtype_data('M', 'F', 'M', 'D');
 576                ev->event.data.alloc.target_lp = target_lp;
 577                ev->event.data.alloc.type = type;
 578                ev->event.data.alloc.count = count;
 579                ev->hdlr = hdlr;
 580                rc = signal_event(ev);
 581        }
 582        if ((rc != 0) && (hdlr != NULL))
 583                (*hdlr)(user_token, rc);
 584}
 585EXPORT_SYMBOL(mf_deallocate_lp_events);
 586
 587/*
 588 * Global kernel interface to tell the VSP object in the primary
 589 * partition to power this partition off.
 590 */
 591void mf_power_off(void)
 592{
 593        printk(KERN_INFO "mf.c: Down it goes...\n");
 594        signal_ce_msg_simple(0x4d, NULL);
 595        for (;;)
 596                ;
 597}
 598
 599/*
 600 * Global kernel interface to tell the VSP object in the primary
 601 * partition to reboot this partition.
 602 */
 603void mf_reboot(char *cmd)
 604{
 605        printk(KERN_INFO "mf.c: Preparing to bounce...\n");
 606        signal_ce_msg_simple(0x4e, NULL);
 607        for (;;)
 608                ;
 609}
 610
 611/*
 612 * Display a single word SRC onto the VSP control panel.
 613 */
 614void mf_display_src(u32 word)
 615{
 616        u8 ce[12];
 617
 618        memset(ce, 0, sizeof(ce));
 619        ce[3] = 0x4a;
 620        ce[7] = 0x01;
 621        ce[8] = word >> 24;
 622        ce[9] = word >> 16;
 623        ce[10] = word >> 8;
 624        ce[11] = word;
 625        signal_ce_msg(ce, NULL);
 626}
 627
 628/*
 629 * Display a single word SRC of the form "PROGXXXX" on the VSP control panel.
 630 */
 631static __init void mf_display_progress_src(u16 value)
 632{
 633        u8 ce[12];
 634        u8 src[72];
 635
 636        memcpy(ce, "\x00\x00\x04\x4A\x00\x00\x00\x48\x00\x00\x00\x00", 12);
 637        memcpy(src, "\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
 638                "\x00\x00\x00\x00\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\x00PROGxxxx                        ",
 641                72);
 642        src[6] = value >> 8;
 643        src[7] = value & 255;
 644        src[44] = "0123456789ABCDEF"[(value >> 12) & 15];
 645        src[45] = "0123456789ABCDEF"[(value >> 8) & 15];
 646        src[46] = "0123456789ABCDEF"[(value >> 4) & 15];
 647        src[47] = "0123456789ABCDEF"[value & 15];
 648        dma_and_signal_ce_msg(ce, NULL, src, sizeof(src), 9 * 64 * 1024);
 649}
 650
 651/*
 652 * Clear the VSP control panel.  Used to "erase" an SRC that was
 653 * previously displayed.
 654 */
 655static void mf_clear_src(void)
 656{
 657        signal_ce_msg_simple(0x4b, NULL);
 658}
 659
 660void __init mf_display_progress(u16 value)
 661{
 662        if (!mf_initialized)
 663                return;
 664
 665        if (0xFFFF == value)
 666                mf_clear_src();
 667        else
 668                mf_display_progress_src(value);
 669}
 670
 671/*
 672 * Initialization code here.
 673 */
 674void __init mf_init(void)
 675{
 676        int i;
 677
 678        spin_lock_init(&pending_event_spinlock);
 679
 680        for (i = 0; i < PENDING_EVENT_PREALLOC_LEN; i++)
 681                free_pending_event(&pending_event_prealloc[i]);
 682
 683        HvLpEvent_registerHandler(HvLpEvent_Type_MachineFac, &hv_handler);
 684
 685        /* virtual continue ack */
 686        signal_ce_msg_simple(0x57, NULL);
 687
 688        mf_initialized = 1;
 689        mb();
 690
 691        printk(KERN_NOTICE "mf.c: iSeries Linux LPAR Machine Facilities "
 692                        "initialized\n");
 693}
 694
 695struct rtc_time_data {
 696        struct completion com;
 697        struct ce_msg_data ce_msg;
 698        int rc;
 699};
 700
 701static void get_rtc_time_complete(void *token, struct ce_msg_data *ce_msg)
 702{
 703        struct rtc_time_data *rtc = token;
 704
 705        memcpy(&rtc->ce_msg, ce_msg, sizeof(rtc->ce_msg));
 706        rtc->rc = 0;
 707        complete(&rtc->com);
 708}
 709
 710static int mf_set_rtc(struct rtc_time *tm)
 711{
 712        char ce_time[12];
 713        u8 day, mon, hour, min, sec, y1, y2;
 714        unsigned year;
 715
 716        year = 1900 + tm->tm_year;
 717        y1 = year / 100;
 718        y2 = year % 100;
 719
 720        sec = tm->tm_sec;
 721        min = tm->tm_min;
 722        hour = tm->tm_hour;
 723        day = tm->tm_mday;
 724        mon = tm->tm_mon + 1;
 725
 726        sec = bin2bcd(sec);
 727        min = bin2bcd(min);
 728        hour = bin2bcd(hour);
 729        mon = bin2bcd(mon);
 730        day = bin2bcd(day);
 731        y1 = bin2bcd(y1);
 732        y2 = bin2bcd(y2);
 733
 734        memset(ce_time, 0, sizeof(ce_time));
 735        ce_time[3] = 0x41;
 736        ce_time[4] = y1;
 737        ce_time[5] = y2;
 738        ce_time[6] = sec;
 739        ce_time[7] = min;
 740        ce_time[8] = hour;
 741        ce_time[10] = day;
 742        ce_time[11] = mon;
 743
 744        return signal_ce_msg(ce_time, NULL);
 745}
 746
 747static int rtc_set_tm(int rc, u8 *ce_msg, struct rtc_time *tm)
 748{
 749        tm->tm_wday = 0;
 750        tm->tm_yday = 0;
 751        tm->tm_isdst = 0;
 752        if (rc) {
 753                tm->tm_sec = 0;
 754                tm->tm_min = 0;
 755                tm->tm_hour = 0;
 756                tm->tm_mday = 15;
 757                tm->tm_mon = 5;
 758                tm->tm_year = 52;
 759                return rc;
 760        }
 761
 762        if ((ce_msg[2] == 0xa9) ||
 763            (ce_msg[2] == 0xaf)) {
 764                /* TOD clock is not set */
 765                tm->tm_sec = 1;
 766                tm->tm_min = 1;
 767                tm->tm_hour = 1;
 768                tm->tm_mday = 10;
 769                tm->tm_mon = 8;
 770                tm->tm_year = 71;
 771                mf_set_rtc(tm);
 772        }
 773        {
 774                u8 year = ce_msg[5];
 775                u8 sec = ce_msg[6];
 776                u8 min = ce_msg[7];
 777                u8 hour = ce_msg[8];
 778                u8 day = ce_msg[10];
 779                u8 mon = ce_msg[11];
 780
 781                sec = bcd2bin(sec);
 782                min = bcd2bin(min);
 783                hour = bcd2bin(hour);
 784                day = bcd2bin(day);
 785                mon = bcd2bin(mon);
 786                year = bcd2bin(year);
 787
 788                if (year <= 69)
 789                        year += 100;
 790
 791                tm->tm_sec = sec;
 792                tm->tm_min = min;
 793                tm->tm_hour = hour;
 794                tm->tm_mday = day;
 795                tm->tm_mon = mon;
 796                tm->tm_year = year;
 797        }
 798
 799        return 0;
 800}
 801
 802static int mf_get_rtc(struct rtc_time *tm)
 803{
 804        struct ce_msg_comp_data ce_complete;
 805        struct rtc_time_data rtc_data;
 806        int rc;
 807
 808        memset(&ce_complete, 0, sizeof(ce_complete));
 809        memset(&rtc_data, 0, sizeof(rtc_data));
 810        init_completion(&rtc_data.com);
 811        ce_complete.handler = &get_rtc_time_complete;
 812        ce_complete.token = &rtc_data;
 813        rc = signal_ce_msg_simple(0x40, &ce_complete);
 814        if (rc)
 815                return rc;
 816        wait_for_completion(&rtc_data.com);
 817        return rtc_set_tm(rtc_data.rc, rtc_data.ce_msg.ce_msg, tm);
 818}
 819
 820struct boot_rtc_time_data {
 821        int busy;
 822        struct ce_msg_data ce_msg;
 823        int rc;
 824};
 825
 826static void get_boot_rtc_time_complete(void *token, struct ce_msg_data *ce_msg)
 827{
 828        struct boot_rtc_time_data *rtc = token;
 829
 830        memcpy(&rtc->ce_msg, ce_msg, sizeof(rtc->ce_msg));
 831        rtc->rc = 0;
 832        rtc->busy = 0;
 833}
 834
 835static int mf_get_boot_rtc(struct rtc_time *tm)
 836{
 837        struct ce_msg_comp_data ce_complete;
 838        struct boot_rtc_time_data rtc_data;
 839        int rc;
 840
 841        memset(&ce_complete, 0, sizeof(ce_complete));
 842        memset(&rtc_data, 0, sizeof(rtc_data));
 843        rtc_data.busy = 1;
 844        ce_complete.handler = &get_boot_rtc_time_complete;
 845        ce_complete.token = &rtc_data;
 846        rc = signal_ce_msg_simple(0x40, &ce_complete);
 847        if (rc)
 848                return rc;
 849        /* We need to poll here as we are not yet taking interrupts */
 850        while (rtc_data.busy) {
 851                if (hvlpevent_is_pending())
 852                        process_hvlpevents();
 853        }
 854        return rtc_set_tm(rtc_data.rc, rtc_data.ce_msg.ce_msg, tm);
 855}
 856
 857#ifdef CONFIG_PROC_FS
 858
 859static int proc_mf_dump_cmdline(char *page, char **start, off_t off,
 860                int count, int *eof, void *data)
 861{
 862        int len;
 863        char *p;
 864        struct vsp_cmd_data vsp_cmd;
 865        int rc;
 866        dma_addr_t dma_addr;
 867
 868        /* The HV appears to return no more than 256 bytes of command line */
 869        if (off >= 256)
 870                return 0;
 871        if ((off + count) > 256)
 872                count = 256 - off;
 873
 874        dma_addr = iseries_hv_map(page, off + count, DMA_FROM_DEVICE);
 875        if (dma_addr == DMA_ERROR_CODE)
 876                return -ENOMEM;
 877        memset(page, 0, off + count);
 878        memset(&vsp_cmd, 0, sizeof(vsp_cmd));
 879        vsp_cmd.cmd = 33;
 880        vsp_cmd.sub_data.kern.token = dma_addr;
 881        vsp_cmd.sub_data.kern.address_type = HvLpDma_AddressType_TceIndex;
 882        vsp_cmd.sub_data.kern.side = (u64)data;
 883        vsp_cmd.sub_data.kern.length = off + count;
 884        mb();
 885        rc = signal_vsp_instruction(&vsp_cmd);
 886        iseries_hv_unmap(dma_addr, off + count, DMA_FROM_DEVICE);
 887        if (rc)
 888                return rc;
 889        if (vsp_cmd.result_code != 0)
 890                return -ENOMEM;
 891        p = page;
 892        len = 0;
 893        while (len < (off + count)) {
 894                if ((*p == '\0') || (*p == '\n')) {
 895                        if (*p == '\0')
 896                                *p = '\n';
 897                        p++;
 898                        len++;
 899                        *eof = 1;
 900                        break;
 901                }
 902                p++;
 903                len++;
 904        }
 905
 906        if (len < off) {
 907                *eof = 1;
 908                len = 0;
 909        }
 910        return len;
 911}
 912
 913#if 0
 914static int mf_getVmlinuxChunk(char *buffer, int *size, int offset, u64 side)
 915{
 916        struct vsp_cmd_data vsp_cmd;
 917        int rc;
 918        int len = *size;
 919        dma_addr_t dma_addr;
 920
 921        dma_addr = iseries_hv_map(buffer, len, DMA_FROM_DEVICE);
 922        memset(buffer, 0, len);
 923        memset(&vsp_cmd, 0, sizeof(vsp_cmd));
 924        vsp_cmd.cmd = 32;
 925        vsp_cmd.sub_data.kern.token = dma_addr;
 926        vsp_cmd.sub_data.kern.address_type = HvLpDma_AddressType_TceIndex;
 927        vsp_cmd.sub_data.kern.side = side;
 928        vsp_cmd.sub_data.kern.offset = offset;
 929        vsp_cmd.sub_data.kern.length = len;
 930        mb();
 931        rc = signal_vsp_instruction(&vsp_cmd);
 932        if (rc == 0) {
 933                if (vsp_cmd.result_code == 0)
 934                        *size = vsp_cmd.sub_data.length_out;
 935                else
 936                        rc = -ENOMEM;
 937        }
 938
 939        iseries_hv_unmap(dma_addr, len, DMA_FROM_DEVICE);
 940
 941        return rc;
 942}
 943
 944static int proc_mf_dump_vmlinux(char *page, char **start, off_t off,
 945                int count, int *eof, void *data)
 946{
 947        int sizeToGet = count;
 948
 949        if (!capable(CAP_SYS_ADMIN))
 950                return -EACCES;
 951
 952        if (mf_getVmlinuxChunk(page, &sizeToGet, off, (u64)data) == 0) {
 953                if (sizeToGet != 0) {
 954                        *start = page + off;
 955                        return sizeToGet;
 956                }
 957                *eof = 1;
 958                return 0;
 959        }
 960        *eof = 1;
 961        return 0;
 962}
 963#endif
 964
 965static int proc_mf_dump_side(char *page, char **start, off_t off,
 966                int count, int *eof, void *data)
 967{
 968        int len;
 969        char mf_current_side = ' ';
 970        struct vsp_cmd_data vsp_cmd;
 971
 972        memset(&vsp_cmd, 0, sizeof(vsp_cmd));
 973        vsp_cmd.cmd = 2;
 974        vsp_cmd.sub_data.ipl_type = 0;
 975        mb();
 976
 977        if (signal_vsp_instruction(&vsp_cmd) == 0) {
 978                if (vsp_cmd.result_code == 0) {
 979                        switch (vsp_cmd.sub_data.ipl_type) {
 980                        case 0: mf_current_side = 'A';
 981                                break;
 982                        case 1: mf_current_side = 'B';
 983                                break;
 984                        case 2: mf_current_side = 'C';
 985                                break;
 986                        default:        mf_current_side = 'D';
 987                                break;
 988                        }
 989                }
 990        }
 991
 992        len = sprintf(page, "%c\n", mf_current_side);
 993
 994        if (len <= (off + count))
 995                *eof = 1;
 996        *start = page + off;
 997        len -= off;
 998        if (len > count)
 999                len = count;
1000        if (len < 0)
1001                len = 0;
1002        return len;
1003}
1004
1005static int proc_mf_change_side(struct file *file, const char __user *buffer,
1006                unsigned long count, void *data)
1007{
1008        char side;
1009        u64 newSide;
1010        struct vsp_cmd_data vsp_cmd;
1011
1012        if (!capable(CAP_SYS_ADMIN))
1013                return -EACCES;
1014
1015        if (count == 0)
1016                return 0;
1017
1018        if (get_user(side, buffer))
1019                return -EFAULT;
1020
1021        switch (side) {
1022        case 'A':       newSide = 0;
1023                        break;
1024        case 'B':       newSide = 1;
1025                        break;
1026        case 'C':       newSide = 2;
1027                        break;
1028        case 'D':       newSide = 3;
1029                        break;
1030        default:
1031                printk(KERN_ERR "mf_proc.c: proc_mf_change_side: invalid side\n");
1032                return -EINVAL;
1033        }
1034
1035        memset(&vsp_cmd, 0, sizeof(vsp_cmd));
1036        vsp_cmd.sub_data.ipl_type = newSide;
1037        vsp_cmd.cmd = 10;
1038
1039        (void)signal_vsp_instruction(&vsp_cmd);
1040
1041        return count;
1042}
1043
1044#if 0
1045static void mf_getSrcHistory(char *buffer, int size)
1046{
1047        struct IplTypeReturnStuff return_stuff;
1048        struct pending_event *ev = new_pending_event();
1049        int rc = 0;
1050        char *pages[4];
1051
1052        pages[0] = kmalloc(4096, GFP_ATOMIC);
1053        pages[1] = kmalloc(4096, GFP_ATOMIC);
1054        pages[2] = kmalloc(4096, GFP_ATOMIC);
1055        pages[3] = kmalloc(4096, GFP_ATOMIC);
1056        if ((ev == NULL) || (pages[0] == NULL) || (pages[1] == NULL)
1057                         || (pages[2] == NULL) || (pages[3] == NULL))
1058                return -ENOMEM;
1059
1060        return_stuff.xType = 0;
1061        return_stuff.xRc = 0;
1062        return_stuff.xDone = 0;
1063        ev->event.hp_lp_event.xSubtype = 6;
1064        ev->event.hp_lp_event.x.xSubtypeData =
1065                subtype_data('M', 'F', 'V', 'I');
1066        ev->event.data.vsp_cmd.xEvent = &return_stuff;
1067        ev->event.data.vsp_cmd.cmd = 4;
1068        ev->event.data.vsp_cmd.lp_index = HvLpConfig_getLpIndex();
1069        ev->event.data.vsp_cmd.result_code = 0xFF;
1070        ev->event.data.vsp_cmd.reserved = 0;
1071        ev->event.data.vsp_cmd.sub_data.page[0] = iseries_hv_addr(pages[0]);
1072        ev->event.data.vsp_cmd.sub_data.page[1] = iseries_hv_addr(pages[1]);
1073        ev->event.data.vsp_cmd.sub_data.page[2] = iseries_hv_addr(pages[2]);
1074        ev->event.data.vsp_cmd.sub_data.page[3] = iseries_hv_addr(pages[3]);
1075        mb();
1076        if (signal_event(ev) != 0)
1077                return;
1078
1079        while (return_stuff.xDone != 1)
1080                udelay(10);
1081        if (return_stuff.xRc == 0)
1082                memcpy(buffer, pages[0], size);
1083        kfree(pages[0]);
1084        kfree(pages[1]);
1085        kfree(pages[2]);
1086        kfree(pages[3]);
1087}
1088#endif
1089
1090static int proc_mf_dump_src(char *page, char **start, off_t off,
1091                int count, int *eof, void *data)
1092{
1093#if 0
1094        int len;
1095
1096        mf_getSrcHistory(page, count);
1097        len = count;
1098        len -= off;
1099        if (len < count) {
1100                *eof = 1;
1101                if (len <= 0)
1102                        return 0;
1103        } else
1104                len = count;
1105        *start = page + off;
1106        return len;
1107#else
1108        return 0;
1109#endif
1110}
1111
1112static int proc_mf_change_src(struct file *file, const char __user *buffer,
1113                unsigned long count, void *data)
1114{
1115        char stkbuf[10];
1116
1117        if (!capable(CAP_SYS_ADMIN))
1118                return -EACCES;
1119
1120        if ((count < 4) && (count != 1)) {
1121                printk(KERN_ERR "mf_proc: invalid src\n");
1122                return -EINVAL;
1123        }
1124
1125        if (count > (sizeof(stkbuf) - 1))
1126                count = sizeof(stkbuf) - 1;
1127        if (copy_from_user(stkbuf, buffer, count))
1128                return -EFAULT;
1129
1130        if ((count == 1) && (*stkbuf == '\0'))
1131                mf_clear_src();
1132        else
1133                mf_display_src(*(u32 *)stkbuf);
1134
1135        return count;
1136}
1137
1138static int proc_mf_change_cmdline(struct file *file, const char __user *buffer,
1139                unsigned long count, void *data)
1140{
1141        struct vsp_cmd_data vsp_cmd;
1142        dma_addr_t dma_addr;
1143        char *page;
1144        int ret = -EACCES;
1145
1146        if (!capable(CAP_SYS_ADMIN))
1147                goto out;
1148
1149        dma_addr = 0;
1150        page = iseries_hv_alloc(count, &dma_addr, GFP_ATOMIC);
1151        ret = -ENOMEM;
1152        if (page == NULL)
1153                goto out;
1154
1155        ret = -EFAULT;
1156        if (copy_from_user(page, buffer, count))
1157                goto out_free;
1158
1159        memset(&vsp_cmd, 0, sizeof(vsp_cmd));
1160        vsp_cmd.cmd = 31;
1161        vsp_cmd.sub_data.kern.token = dma_addr;
1162        vsp_cmd.sub_data.kern.address_type = HvLpDma_AddressType_TceIndex;
1163        vsp_cmd.sub_data.kern.side = (u64)data;
1164        vsp_cmd.sub_data.kern.length = count;
1165        mb();
1166        (void)signal_vsp_instruction(&vsp_cmd);
1167        ret = count;
1168
1169out_free:
1170        iseries_hv_free(count, page, dma_addr);
1171out:
1172        return ret;
1173}
1174
1175static ssize_t proc_mf_change_vmlinux(struct file *file,
1176                                      const char __user *buf,
1177                                      size_t count, loff_t *ppos)
1178{
1179        struct proc_dir_entry *dp = PDE(file->f_path.dentry->d_inode);
1180        ssize_t rc;
1181        dma_addr_t dma_addr;
1182        char *page;
1183        struct vsp_cmd_data vsp_cmd;
1184
1185        rc = -EACCES;
1186        if (!capable(CAP_SYS_ADMIN))
1187                goto out;
1188
1189        dma_addr = 0;
1190        page = iseries_hv_alloc(count, &dma_addr, GFP_ATOMIC);
1191        rc = -ENOMEM;
1192        if (page == NULL) {
1193                printk(KERN_ERR "mf.c: couldn't allocate memory to set vmlinux chunk\n");
1194                goto out;
1195        }
1196        rc = -EFAULT;
1197        if (copy_from_user(page, buf, count))
1198                goto out_free;
1199
1200        memset(&vsp_cmd, 0, sizeof(vsp_cmd));
1201        vsp_cmd.cmd = 30;
1202        vsp_cmd.sub_data.kern.token = dma_addr;
1203        vsp_cmd.sub_data.kern.address_type = HvLpDma_AddressType_TceIndex;
1204        vsp_cmd.sub_data.kern.side = (u64)dp->data;
1205        vsp_cmd.sub_data.kern.offset = *ppos;
1206        vsp_cmd.sub_data.kern.length = count;
1207        mb();
1208        rc = signal_vsp_instruction(&vsp_cmd);
1209        if (rc)
1210                goto out_free;
1211        rc = -ENOMEM;
1212        if (vsp_cmd.result_code != 0)
1213                goto out_free;
1214
1215        *ppos += count;
1216        rc = count;
1217out_free:
1218        iseries_hv_free(count, page, dma_addr);
1219out:
1220        return rc;
1221}
1222
1223static const struct file_operations proc_vmlinux_operations = {
1224        .write          = proc_mf_change_vmlinux,
1225};
1226
1227static int __init mf_proc_init(void)
1228{
1229        struct proc_dir_entry *mf_proc_root;
1230        struct proc_dir_entry *ent;
1231        struct proc_dir_entry *mf;
1232        char name[2];
1233        int i;
1234
1235        if (!firmware_has_feature(FW_FEATURE_ISERIES))
1236                return 0;
1237
1238        mf_proc_root = proc_mkdir("iSeries/mf", NULL);
1239        if (!mf_proc_root)
1240                return 1;
1241
1242        name[1] = '\0';
1243        for (i = 0; i < 4; i++) {
1244                name[0] = 'A' + i;
1245                mf = proc_mkdir(name, mf_proc_root);
1246                if (!mf)
1247                        return 1;
1248
1249                ent = create_proc_entry("cmdline", S_IFREG|S_IRUSR|S_IWUSR, mf);
1250                if (!ent)
1251                        return 1;
1252                ent->data = (void *)(long)i;
1253                ent->read_proc = proc_mf_dump_cmdline;
1254                ent->write_proc = proc_mf_change_cmdline;
1255
1256                if (i == 3)     /* no vmlinux entry for 'D' */
1257                        continue;
1258
1259                ent = proc_create_data("vmlinux", S_IFREG|S_IWUSR, mf,
1260                                       &proc_vmlinux_operations,
1261                                       (void *)(long)i);
1262                if (!ent)
1263                        return 1;
1264        }
1265
1266        ent = create_proc_entry("side", S_IFREG|S_IRUSR|S_IWUSR, mf_proc_root);
1267        if (!ent)
1268                return 1;
1269        ent->data = (void *)0;
1270        ent->read_proc = proc_mf_dump_side;
1271        ent->write_proc = proc_mf_change_side;
1272
1273        ent = create_proc_entry("src", S_IFREG|S_IRUSR|S_IWUSR, mf_proc_root);
1274        if (!ent)
1275                return 1;
1276        ent->data = (void *)0;
1277        ent->read_proc = proc_mf_dump_src;
1278        ent->write_proc = proc_mf_change_src;
1279
1280        return 0;
1281}
1282
1283__initcall(mf_proc_init);
1284
1285#endif /* CONFIG_PROC_FS */
1286
1287/*
1288 * Get the RTC from the virtual service processor
1289 * This requires flowing LpEvents to the primary partition
1290 */
1291void iSeries_get_rtc_time(struct rtc_time *rtc_tm)
1292{
1293        mf_get_rtc(rtc_tm);
1294        rtc_tm->tm_mon--;
1295}
1296
1297/*
1298 * Set the RTC in the virtual service processor
1299 * This requires flowing LpEvents to the primary partition
1300 */
1301int iSeries_set_rtc_time(struct rtc_time *tm)
1302{
1303        mf_set_rtc(tm);
1304        return 0;
1305}
1306
1307unsigned long iSeries_get_boot_time(void)
1308{
1309        struct rtc_time tm;
1310
1311        mf_get_boot_rtc(&tm);
1312        return mktime(tm.tm_year + 1900, tm.tm_mon, tm.tm_mday,
1313                      tm.tm_hour, tm.tm_min, tm.tm_sec);
1314}
1315