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