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