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