ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc64 / kernel / mf.c
1 /*
2   * mf.c
3   * Copyright (C) 2001 Troy D. Armstrong  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 <asm/iSeries/mf.h>
28 #include <linux/types.h>
29 #include <linux/errno.h>
30 #include <linux/kernel.h>
31 #include <linux/init.h>
32 #include <linux/mm.h>
33 #include <linux/module.h>
34 #include <linux/completion.h>
35 #include <asm/iSeries/HvLpConfig.h>
36 #include <linux/slab.h>
37 #include <linux/delay.h>
38 #include <asm/nvram.h>
39 #include <asm/time.h>
40 #include <asm/iSeries/ItSpCommArea.h>
41 #include <asm/uaccess.h>
42 #include <linux/dma-mapping.h>
43 #include <linux/bcd.h>
44 #include <asm/iSeries/vio.h>
45
46 /*
47  * This is the structure layout for the Machine Facilites LPAR event
48  * flows.
49  */
50 union safe_cast {
51         u64 ptr_as_u64;
52         void *ptr;
53 };
54
55 struct VspCmdData {
56         union safe_cast token;
57         u16 cmd;
58         HvLpIndex lp_index;
59         u8 result_code;
60         u32 reserved;
61         union {
62                 u64 state;      /* GetStateOut */
63                 u64 ipl_type;   /* GetIplTypeOut, Function02SelectIplTypeIn */
64                 u64 ipl_mode;   /* GetIplModeOut, Function02SelectIplModeIn */
65                 u64 page[4];    /* GetSrcHistoryIn */
66                 u64 flag;       /* GetAutoIplWhenPrimaryIplsOut,
67                                    SetAutoIplWhenPrimaryIplsIn,
68                                    WhiteButtonPowerOffIn,
69                                    Function08FastPowerOffIn,
70                                    IsSpcnRackPowerIncompleteOut */
71                 struct {
72                         u64 token;
73                         u64 address_type;
74                         u64 side;
75                         u32 length;
76                         u32 offset;
77                 } kern;         /* SetKernelImageIn, GetKernelImageIn,
78                                    SetKernelCmdLineIn, GetKernelCmdLineIn */
79                 u32 length_out; /* GetKernelImageOut, GetKernelCmdLineOut */
80                 u8 reserved[80];
81         } sub_data;
82 };
83
84 struct VspRspData {
85         struct completion com;
86         struct VspCmdData *response;
87 };
88
89 struct AllocData {
90         u16 size;
91         u16 type;
92         u32 count;
93         u16 reserved1;
94         u8 reserved2;
95         HvLpIndex target_lp;
96 };
97
98 struct CeMsgData;
99
100 typedef void (*CeMsgCompleteHandler)(void *token, struct CeMsgData *vspCmdRsp);
101
102 struct CeMsgCompleteData {
103         CeMsgCompleteHandler handler;
104         void *token;
105 };
106
107 struct CeMsgData {
108         u8 ce_msg[12];
109         char reserved[4];
110         struct CeMsgCompleteData *completion;
111 };
112
113 struct IoMFLpEvent {
114         struct HvLpEvent hp_lp_event;
115         u16 subtype_result_code;
116         u16 reserved1;
117         u32 reserved2;
118         union {
119                 struct AllocData alloc;
120                 struct CeMsgData ce_msg;
121                 struct VspCmdData vsp_cmd;
122         } data;
123 };
124
125 #define subtype_data(a, b, c, d)        \
126                 (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
127
128 /*
129  * All outgoing event traffic is kept on a FIFO queue.  The first
130  * pointer points to the one that is outstanding, and all new
131  * requests get stuck on the end.  Also, we keep a certain number of
132  * preallocated pending events so that we can operate very early in
133  * the boot up sequence (before kmalloc is ready).
134  */
135 struct pending_event {
136         struct pending_event *next;
137         struct IoMFLpEvent event;
138         MFCompleteHandler hdlr;
139         char dma_data[72];
140         unsigned dma_data_length;
141         unsigned remote_address;
142 };
143 static spinlock_t pending_event_spinlock;
144 static struct pending_event *pending_event_head;
145 static struct pending_event *pending_event_tail;
146 static struct pending_event *pending_event_avail;
147 static struct pending_event pending_event_prealloc[16];
148
149 /*
150  * Put a pending event onto the available queue, so it can get reused.
151  * Attention! You must have the pending_event_spinlock before calling!
152  */
153 static void free_pending_event(struct pending_event *ev)
154 {
155         if (ev != NULL) {
156                 ev->next = pending_event_avail;
157                 pending_event_avail = ev;
158         }
159 }
160
161 /*
162  * Enqueue the outbound event onto the stack.  If the queue was
163  * empty to begin with, we must also issue it via the Hypervisor
164  * interface.  There is a section of code below that will touch
165  * the first stack pointer without the protection of the pending_event_spinlock.
166  * This is OK, because we know that nobody else will be modifying
167  * the first pointer when we do this.
168  */
169 static int signal_event(struct pending_event *ev)
170 {
171         int rc = 0;
172         unsigned long flags;
173         int go = 1;
174         struct pending_event *ev1;
175         HvLpEvent_Rc hvRc;
176
177         /* enqueue the event */
178         if (ev != NULL) {
179                 ev->next = NULL;
180                 spin_lock_irqsave(&pending_event_spinlock, flags);
181                 if (pending_event_head == NULL)
182                         pending_event_head = ev;
183                 else {
184                         go = 0;
185                         pending_event_tail->next = ev;
186                 }
187                 pending_event_tail = ev;
188                 spin_unlock_irqrestore(&pending_event_spinlock, flags);
189         }
190
191         /* send the event */
192         while (go) {
193                 go = 0;
194
195                 /* any DMA data to send beforehand? */
196                 if (pending_event_head->dma_data_length > 0)
197                         HvCallEvent_dmaToSp(pending_event_head->dma_data,
198                                         pending_event_head->remote_address,
199                                         pending_event_head->dma_data_length,
200                                         HvLpDma_Direction_LocalToRemote);
201
202                 hvRc = HvCallEvent_signalLpEvent(
203                                 &pending_event_head->event.hp_lp_event);
204                 if (hvRc != HvLpEvent_Rc_Good) {
205                         printk(KERN_ERR "mf.c: HvCallEvent_signalLpEvent() failed with %d\n",
206                                         (int)hvRc);
207
208                         spin_lock_irqsave(&pending_event_spinlock, flags);
209                         ev1 = pending_event_head;
210                         pending_event_head = pending_event_head->next;
211                         if (pending_event_head != NULL)
212                                 go = 1;
213                         spin_unlock_irqrestore(&pending_event_spinlock, flags);
214
215                         if (ev1 == ev)
216                                 rc = -EIO;
217                         else if (ev1->hdlr != NULL) {
218                                 union safe_cast mySafeCast;
219
220                                 mySafeCast.ptr_as_u64 = ev1->event.hp_lp_event.xCorrelationToken;
221                                 (*ev1->hdlr)(mySafeCast.ptr, -EIO);
222                         }
223
224                         spin_lock_irqsave(&pending_event_spinlock, flags);
225                         free_pending_event(ev1);
226                         spin_unlock_irqrestore(&pending_event_spinlock, flags);
227                 }
228         }
229
230         return rc;
231 }
232
233 /*
234  * Allocate a new pending_event structure, and initialize it.
235  */
236 static struct pending_event *new_pending_event(void)
237 {
238         struct pending_event *ev = NULL;
239         HvLpIndex primaryLp = HvLpConfig_getPrimaryLpIndex();
240         unsigned long flags;
241         struct HvLpEvent *hev;
242
243         spin_lock_irqsave(&pending_event_spinlock, flags);
244         if (pending_event_avail != NULL) {
245                 ev = pending_event_avail;
246                 pending_event_avail = pending_event_avail->next;
247         }
248         spin_unlock_irqrestore(&pending_event_spinlock, flags);
249         if (ev == NULL)
250                 ev = kmalloc(sizeof(struct pending_event),GFP_ATOMIC);
251         if (ev == NULL) {
252                 printk(KERN_ERR "mf.c: unable to kmalloc %ld bytes\n",
253                                 sizeof(struct pending_event));
254                 return NULL;
255         }
256         memset(ev, 0, sizeof(struct pending_event));
257         hev = &ev->event.hp_lp_event;
258         hev->xFlags.xValid = 1;
259         hev->xFlags.xAckType = HvLpEvent_AckType_ImmediateAck;
260         hev->xFlags.xAckInd = HvLpEvent_AckInd_DoAck;
261         hev->xFlags.xFunction = HvLpEvent_Function_Int;
262         hev->xType = HvLpEvent_Type_MachineFac;
263         hev->xSourceLp = HvLpConfig_getLpIndex();
264         hev->xTargetLp = primaryLp;
265         hev->xSizeMinus1 = sizeof(ev->event)-1;
266         hev->xRc = HvLpEvent_Rc_Good;
267         hev->xSourceInstanceId = HvCallEvent_getSourceLpInstanceId(primaryLp,
268                         HvLpEvent_Type_MachineFac);
269         hev->xTargetInstanceId = HvCallEvent_getTargetLpInstanceId(primaryLp,
270                         HvLpEvent_Type_MachineFac);
271
272         return ev;
273 }
274
275 static int signal_vsp_instruction(struct VspCmdData *vspCmd)
276 {
277         struct pending_event *ev = new_pending_event();
278         int rc;
279         struct VspRspData response;
280
281         if (ev == NULL)
282                 return -ENOMEM;
283
284         init_completion(&response.com);
285         response.response = vspCmd;
286         ev->event.hp_lp_event.xSubtype = 6;
287         ev->event.hp_lp_event.x.xSubtypeData =
288                 subtype_data('M', 'F',  'V',  'I');
289         ev->event.data.vsp_cmd.token.ptr = &response;
290         ev->event.data.vsp_cmd.cmd = vspCmd->cmd;
291         ev->event.data.vsp_cmd.lp_index = HvLpConfig_getLpIndex();
292         ev->event.data.vsp_cmd.result_code = 0xFF;
293         ev->event.data.vsp_cmd.reserved = 0;
294         memcpy(&(ev->event.data.vsp_cmd.sub_data),
295                         &(vspCmd->sub_data), sizeof(vspCmd->sub_data));
296         mb();
297
298         rc = signal_event(ev);
299         if (rc == 0)
300                 wait_for_completion(&response.com);
301         return rc;
302 }
303
304
305 /*
306  * Send a 12-byte CE message to the primary partition VSP object
307  */
308 static int signal_ce_msg(char *ce_msg, struct CeMsgCompleteData *completion)
309 {
310         struct pending_event *ev = new_pending_event();
311
312         if (ev == NULL)
313                 return -ENOMEM;
314
315         ev->event.hp_lp_event.xSubtype = 0;
316         ev->event.hp_lp_event.x.xSubtypeData =
317                 subtype_data('M',  'F',  'C',  'E');
318         memcpy(ev->event.data.ce_msg.ce_msg, ce_msg, 12);
319         ev->event.data.ce_msg.completion = completion;
320         return signal_event(ev);
321 }
322
323 /*
324  * Send a 12-byte CE message and DMA data to the primary partition VSP object
325  */
326 static int dma_and_signal_ce_msg(char *ce_msg,
327                 struct CeMsgCompleteData *completion, void *dma_data,
328                 unsigned dma_data_length, unsigned remote_address)
329 {
330         struct pending_event *ev = new_pending_event();
331
332         if (ev == NULL)
333                 return -ENOMEM;
334
335         ev->event.hp_lp_event.xSubtype = 0;
336         ev->event.hp_lp_event.x.xSubtypeData =
337                 subtype_data('M', 'F', 'C', 'E');
338         memcpy(ev->event.data.ce_msg.ce_msg, ce_msg, 12);
339         ev->event.data.ce_msg.completion = completion;
340         memcpy(ev->dma_data, dma_data, dma_data_length);
341         ev->dma_data_length = dma_data_length;
342         ev->remote_address = remote_address;
343         return signal_event(ev);
344 }
345
346 /*
347  * Initiate a nice (hopefully) shutdown of Linux.  We simply are
348  * going to try and send the init process a SIGINT signal.  If
349  * this fails (why?), we'll simply force it off in a not-so-nice
350  * manner.
351  */
352 static int shutdown(void)
353 {
354         int rc = kill_proc(1, SIGINT, 1);
355
356         if (rc) {
357                 printk(KERN_ALERT "mf.c: SIGINT to init failed (%d), "
358                                 "hard shutdown commencing\n", rc);
359                 mf_powerOff();
360         } else
361                 printk(KERN_INFO "mf.c: init has been successfully notified "
362                                 "to proceed with shutdown\n");
363         return rc;
364 }
365
366 /*
367  * The primary partition VSP object is sending us a new
368  * event flow.  Handle it...
369  */
370 static void intReceived(struct IoMFLpEvent *event)
371 {
372         int freeIt = 0;
373         struct pending_event *two = NULL;
374
375         /* ack the interrupt */
376         event->hp_lp_event.xRc = HvLpEvent_Rc_Good;
377         HvCallEvent_ackLpEvent(&event->hp_lp_event);
378
379         /* process interrupt */
380         switch (event->hp_lp_event.xSubtype) {
381         case 0: /* CE message */
382                 switch (event->data.ce_msg.ce_msg[3]) {
383                 case 0x5B:      /* power control notification */
384                         if ((event->data.ce_msg.ce_msg[5] & 0x20) != 0) {
385                                 printk(KERN_INFO "mf.c: Commencing partition shutdown\n");
386                                 if (shutdown() == 0)
387                                         signal_ce_msg("\x00\x00\x00\xDB\x00\x00\x00\x00\x00\x00\x00\x00", NULL);
388                         }
389                         break;
390                 case 0xC0:      /* get time */
391                         if ((pending_event_head == NULL) ||
392                             (pending_event_head->event.data.ce_msg.ce_msg[3]
393                              != 0x40))
394                                 break;
395                         freeIt = 1;
396                         if (pending_event_head->event.data.ce_msg.completion != 0) {
397                                 CeMsgCompleteHandler handler = pending_event_head->event.data.ce_msg.completion->handler;
398                                 void *token = pending_event_head->event.data.ce_msg.completion->token;
399
400                                 if (handler != NULL)
401                                         (*handler)(token, &(event->data.ce_msg));
402                         }
403                         break;
404                 }
405
406                 /* remove from queue */
407                 if (freeIt == 1) {
408                         unsigned long flags;
409
410                         spin_lock_irqsave(&pending_event_spinlock, flags);
411                         if (pending_event_head != NULL) {
412                                 struct pending_event *oldHead =
413                                         pending_event_head;
414
415                                 pending_event_head = pending_event_head->next;
416                                 two = pending_event_head;
417                                 free_pending_event(oldHead);
418                         }
419                         spin_unlock_irqrestore(&pending_event_spinlock, flags);
420                 }
421
422                 /* send next waiting event */
423                 if (two != NULL)
424                         signal_event(NULL);
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 ackReceived(struct IoMFLpEvent *event)
439 {
440         unsigned long flags;
441         struct pending_event * two = NULL;
442         unsigned long freeIt = 0;
443
444         /* handle current event */
445         if (pending_event_head != NULL) {
446                 switch (event->hp_lp_event.xSubtype) {
447                 case 0:     /* CE msg */
448                         if (event->data.ce_msg.ce_msg[3] == 0x40) {
449                                 if (event->data.ce_msg.ce_msg[2] != 0) {
450                                         freeIt = 1;
451                                         if (pending_event_head->event.data.ce_msg.completion
452                                                         != 0) {
453                                                 CeMsgCompleteHandler handler = pending_event_head->event.data.ce_msg.completion->handler;
454                                                 void *token = pending_event_head->event.data.ce_msg.completion->token;
455
456                                                 if (handler != NULL)
457                                                         (*handler)(token, &(event->data.ce_msg));
458                                         }
459                                 }
460                         } else
461                                 freeIt = 1;
462                         break;
463                 case 4: /* allocate */
464                 case 5: /* deallocate */
465                         if (pending_event_head->hdlr != NULL) {
466                                 union safe_cast mySafeCast;
467
468                                 mySafeCast.ptr_as_u64 = event->hp_lp_event.xCorrelationToken;
469                                 (*pending_event_head->hdlr)(mySafeCast.ptr, event->data.alloc.count);
470                         }
471                         freeIt = 1;
472                         break;
473                 case 6:
474                         {
475                                 struct VspRspData *rsp = (struct VspRspData *)event->data.vsp_cmd.token.ptr;
476
477                                 if (rsp != NULL) {
478                                         if (rsp->response != NULL)
479                                                 memcpy(rsp->response, &(event->data.vsp_cmd), sizeof(event->data.vsp_cmd));
480                                         complete(&rsp->com);
481                                 } else
482                                         printk(KERN_ERR "mf.c: no rsp\n");
483                                 freeIt = 1;
484                         }
485                         break;
486                 }
487         }
488         else
489                 printk(KERN_ERR "mf.c: stack empty for receiving ack\n");
490
491         /* remove from queue */
492         spin_lock_irqsave(&pending_event_spinlock, flags);
493         if ((pending_event_head != NULL) && (freeIt == 1)) {
494                 struct pending_event *oldHead = pending_event_head;
495
496                 pending_event_head = pending_event_head->next;
497                 two = pending_event_head;
498                 free_pending_event(oldHead);
499         } 
500         spin_unlock_irqrestore(&pending_event_spinlock, flags);
501
502         /* send next waiting event */
503         if (two != NULL)
504                 signal_event(NULL);
505 }
506
507 /*
508  * This is the generic event handler we are registering with
509  * the Hypervisor.  Ensure the flows are for us, and then
510  * parse it enough to know if it is an interrupt or an
511  * acknowledge.
512  */
513 static void hvHandler(struct HvLpEvent *event, struct pt_regs *regs)
514 {
515         if ((event != NULL) && (event->xType == HvLpEvent_Type_MachineFac)) {
516                 switch(event->xFlags.xFunction) {
517                 case HvLpEvent_Function_Ack:
518                         ackReceived((struct IoMFLpEvent *)event);
519                         break;
520                 case HvLpEvent_Function_Int:
521                         intReceived((struct IoMFLpEvent *)event);
522                         break;
523                 default:
524                         printk(KERN_ERR "mf.c: non ack/int event received\n");
525                         break;
526                 }
527         } else
528                 printk(KERN_ERR "mf.c: alien event received\n");
529 }
530
531 /*
532  * Global kernel interface to allocate and seed events into the
533  * Hypervisor.
534  */
535 void mf_allocateLpEvents(HvLpIndex targetLp, HvLpEvent_Type type,
536                 unsigned size, unsigned count, MFCompleteHandler hdlr,
537                 void *userToken)
538 {
539         struct pending_event *ev = new_pending_event();
540         int rc;
541
542         if (ev == NULL) {
543                 rc = -ENOMEM;
544         } else {
545                 union safe_cast mine;
546
547                 mine.ptr = userToken;
548                 ev->event.hp_lp_event.xSubtype = 4;
549                 ev->event.hp_lp_event.xCorrelationToken = mine.ptr_as_u64;
550                 ev->event.hp_lp_event.x.xSubtypeData =
551                         subtype_data('M', 'F', 'M', 'A');
552                 ev->event.data.alloc.target_lp = targetLp;
553                 ev->event.data.alloc.type = type;
554                 ev->event.data.alloc.size = size;
555                 ev->event.data.alloc.count = count;
556                 ev->hdlr = hdlr;
557                 rc = signal_event(ev);
558         }
559         if ((rc != 0) && (hdlr != NULL))
560                 (*hdlr)(userToken, rc);
561 }
562 EXPORT_SYMBOL(mf_allocateLpEvents);
563
564 /*
565  * Global kernel interface to unseed and deallocate events already in
566  * Hypervisor.
567  */
568 void mf_deallocateLpEvents(HvLpIndex targetLp, HvLpEvent_Type type,
569                 unsigned count, MFCompleteHandler hdlr, void *userToken)
570 {
571         struct pending_event *ev = new_pending_event();
572         int rc;
573
574         if (ev == NULL)
575                 rc = -ENOMEM;
576         else {
577                 union safe_cast mine;
578
579                 mine.ptr = userToken;
580                 ev->event.hp_lp_event.xSubtype = 5;
581                 ev->event.hp_lp_event.xCorrelationToken = mine.ptr_as_u64;
582                 ev->event.hp_lp_event.x.xSubtypeData =
583                         subtype_data('M', 'F', 'M', 'D');
584                 ev->event.data.alloc.target_lp = targetLp;
585                 ev->event.data.alloc.type = type;
586                 ev->event.data.alloc.count = count;
587                 ev->hdlr = hdlr;
588                 rc = signal_event(ev);
589         }
590         if ((rc != 0) && (hdlr != NULL))
591                 (*hdlr)(userToken, rc);
592 }
593 EXPORT_SYMBOL(mf_deallocateLpEvents);
594
595 /*
596  * Global kernel interface to tell the VSP object in the primary
597  * partition to power this partition off.
598  */
599 void mf_powerOff(void)
600 {
601         printk(KERN_INFO "mf.c: Down it goes...\n");
602         signal_ce_msg("\x00\x00\x00\x4D\x00\x00\x00\x00\x00\x00\x00\x00", NULL);
603         for (;;);
604 }
605
606 /*
607  * Global kernel interface to tell the VSP object in the primary
608  * partition to reboot this partition.
609  */
610 void mf_reboot(void)
611 {
612         printk(KERN_INFO "mf.c: Preparing to bounce...\n");
613         signal_ce_msg("\x00\x00\x00\x4E\x00\x00\x00\x00\x00\x00\x00\x00", NULL);
614         for (;;);
615 }
616
617 /*
618  * Display a single word SRC onto the VSP control panel.
619  */
620 void mf_displaySrc(u32 word)
621 {
622         u8 ce[12];
623
624         memcpy(ce, "\x00\x00\x00\x4A\x00\x00\x00\x01\x00\x00\x00\x00", 12);
625         ce[8] = word >> 24;
626         ce[9] = word >> 16;
627         ce[10] = word >> 8;
628         ce[11] = word;
629         signal_ce_msg(ce, NULL);
630 }
631
632 /*
633  * Display a single word SRC of the form "PROGXXXX" on the VSP control panel.
634  */
635 void mf_displayProgress(u16 value)
636 {
637         u8 ce[12];
638         u8 src[72];
639
640         memcpy(ce, "\x00\x00\x04\x4A\x00\x00\x00\x48\x00\x00\x00\x00", 12);
641         memcpy(src, "\x01\x00\x00\x01\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\x00\x00\x00\x00\x00\x00\x00\x00\x00"
644                 "\x00\x00\x00\x00PROGxxxx                        ",
645                 72);
646         src[6] = value >> 8;
647         src[7] = value & 255;
648         src[44] = "0123456789ABCDEF"[(value >> 12) & 15];
649         src[45] = "0123456789ABCDEF"[(value >> 8) & 15];
650         src[46] = "0123456789ABCDEF"[(value >> 4) & 15];
651         src[47] = "0123456789ABCDEF"[value & 15];
652         dma_and_signal_ce_msg(ce, NULL, src, sizeof(src), 9 * 64 * 1024);
653 }
654
655 /*
656  * Clear the VSP control panel.  Used to "erase" an SRC that was
657  * previously displayed.
658  */
659 void mf_clearSrc(void)
660 {
661         signal_ce_msg("\x00\x00\x00\x4B\x00\x00\x00\x00\x00\x00\x00\x00", NULL);
662 }
663
664 /*
665  * Initialization code here.
666  */
667 void mf_init(void)
668 {
669         int i;
670
671         /* initialize */
672         spin_lock_init(&pending_event_spinlock);
673         for (i = 0;
674              i < sizeof(pending_event_prealloc) / sizeof(*pending_event_prealloc);
675              ++i)
676                 free_pending_event(&pending_event_prealloc[i]);
677         HvLpEvent_registerHandler(HvLpEvent_Type_MachineFac, &hvHandler);
678
679         /* virtual continue ack */
680         signal_ce_msg("\x00\x00\x00\x57\x00\x00\x00\x00\x00\x00\x00\x00", NULL);
681
682         /* initialization complete */
683         printk(KERN_NOTICE "mf.c: iSeries Linux LPAR Machine Facilities initialized\n");
684 }
685
686 void mf_setSide(char side)
687 {
688         u64 newSide;
689         struct VspCmdData myVspCmd;
690
691         memset(&myVspCmd, 0, sizeof(myVspCmd));
692         switch (side) {
693         case 'A':       newSide = 0;
694                         break;
695         case 'B':       newSide = 1;
696                         break;
697         case 'C':       newSide = 2; 
698                         break;
699         default:        newSide = 3;
700                         break;
701         }
702         myVspCmd.sub_data.ipl_type = newSide;
703         myVspCmd.cmd = 10;
704
705         (void)signal_vsp_instruction(&myVspCmd);
706 }
707
708 char mf_getSide(void)
709 {
710         char returnValue = ' ';
711         int rc = 0;
712         struct VspCmdData myVspCmd;
713
714         memset(&myVspCmd, 0, sizeof(myVspCmd));
715         myVspCmd.cmd = 2;
716         myVspCmd.sub_data.ipl_type = 0;
717         mb();
718         rc = signal_vsp_instruction(&myVspCmd);
719
720         if (rc != 0)
721                 return returnValue;
722
723         if (myVspCmd.result_code == 0) {
724                 switch (myVspCmd.sub_data.ipl_type) {
725                 case 0: returnValue = 'A';
726                         break;
727                 case 1: returnValue = 'B';
728                         break;
729                 case 2: returnValue = 'C';
730                         break;
731                 default:        returnValue = 'D';
732                         break;
733                 }
734         }
735         return returnValue;
736 }
737
738 void mf_getSrcHistory(char *buffer, int size)
739 {
740 #if 0
741         struct IplTypeReturnStuff returnStuff;
742         struct pending_event *ev = new_pending_event();
743         int rc = 0;
744         char *pages[4];
745
746         pages[0] = kmalloc(4096, GFP_ATOMIC);
747         pages[1] = kmalloc(4096, GFP_ATOMIC);
748         pages[2] = kmalloc(4096, GFP_ATOMIC);
749         pages[3] = kmalloc(4096, GFP_ATOMIC);
750         if ((ev == NULL) || (pages[0] == NULL) || (pages[1] == NULL)
751                          || (pages[2] == NULL) || (pages[3] == NULL))
752                 return -ENOMEM;
753
754         returnStuff.xType = 0;
755         returnStuff.xRc = 0;
756         returnStuff.xDone = 0;
757         ev->event.hp_lp_event.xSubtype = 6;
758         ev->event.hp_lp_event.x.xSubtypeData =
759                 subtype_data('M', 'F', 'V', 'I');
760         ev->event.data.vsp_cmd.xEvent = &returnStuff;
761         ev->event.data.vsp_cmd.cmd = 4;
762         ev->event.data.vsp_cmd.lp_index = HvLpConfig_getLpIndex();
763         ev->event.data.vsp_cmd.result_code = 0xFF;
764         ev->event.data.vsp_cmd.reserved = 0;
765         ev->event.data.vsp_cmd.sub_data.page[0] = ISERIES_HV_ADDR(pages[0]);
766         ev->event.data.vsp_cmd.sub_data.page[1] = ISERIES_HV_ADDR(pages[1]);
767         ev->event.data.vsp_cmd.sub_data.page[2] = ISERIES_HV_ADDR(pages[2]);
768         ev->event.data.vsp_cmd.sub_data.page[3] = ISERIES_HV_ADDR(pages[3]);
769         mb();
770         if (signal_event(ev) != 0)
771                 return;
772
773         while (returnStuff.xDone != 1)
774                 udelay(10);
775         if (returnStuff.xRc == 0)
776                 memcpy(buffer, pages[0], size);
777         kfree(pages[0]);
778         kfree(pages[1]);
779         kfree(pages[2]);
780         kfree(pages[3]);
781 #endif
782 }
783
784 void mf_setCmdLine(const char *cmdline, int size, u64 side)
785 {
786         struct VspCmdData myVspCmd;
787         dma_addr_t dma_addr = 0;
788         char *page = dma_alloc_coherent(iSeries_vio_dev, size, &dma_addr,
789                         GFP_ATOMIC);
790
791         if (page == NULL) {
792                 printk(KERN_ERR "mf.c: couldn't allocate memory to set command line\n");
793                 return;
794         }
795
796         copy_from_user(page, cmdline, size);
797
798         memset(&myVspCmd, 0, sizeof(myVspCmd));
799         myVspCmd.cmd = 31;
800         myVspCmd.sub_data.kern.token = dma_addr;
801         myVspCmd.sub_data.kern.address_type = HvLpDma_AddressType_TceIndex;
802         myVspCmd.sub_data.kern.side = side;
803         myVspCmd.sub_data.kern.length = size;
804         mb();
805         (void)signal_vsp_instruction(&myVspCmd);
806
807         dma_free_coherent(iSeries_vio_dev, size, page, dma_addr);
808 }
809
810 int mf_getCmdLine(char *cmdline, int *size, u64 side)
811 {
812         struct VspCmdData myVspCmd;
813         int rc;
814         int len = *size;
815         dma_addr_t dma_addr;
816
817         dma_addr = dma_map_single(iSeries_vio_dev, cmdline, len,
818                         DMA_FROM_DEVICE);
819         memset(cmdline, 0, len);
820         memset(&myVspCmd, 0, sizeof(myVspCmd));
821         myVspCmd.cmd = 33;
822         myVspCmd.sub_data.kern.token = dma_addr;
823         myVspCmd.sub_data.kern.address_type = HvLpDma_AddressType_TceIndex;
824         myVspCmd.sub_data.kern.side = side;
825         myVspCmd.sub_data.kern.length = len;
826         mb();
827         rc = signal_vsp_instruction(&myVspCmd);
828
829         if (rc == 0) {
830                 if (myVspCmd.result_code == 0)
831                         len = myVspCmd.sub_data.length_out;
832 #if 0
833                 else
834                         memcpy(cmdline, "Bad cmdline", 11);
835 #endif
836         }
837
838         dma_unmap_single(iSeries_vio_dev, dma_addr, *size, DMA_FROM_DEVICE);
839
840         return len;
841 }
842
843
844 int mf_setVmlinuxChunk(const char *buffer, int size, int offset, u64 side)
845 {
846         struct VspCmdData myVspCmd;
847         int rc;
848         dma_addr_t dma_addr = 0;
849         char *page = dma_alloc_coherent(iSeries_vio_dev, size, &dma_addr,
850                         GFP_ATOMIC);
851
852         if (page == NULL) {
853                 printk(KERN_ERR "mf.c: couldn't allocate memory to set vmlinux chunk\n");
854                 return -ENOMEM;
855         }
856
857         copy_from_user(page, buffer, size);
858         memset(&myVspCmd, 0, sizeof(myVspCmd));
859
860         myVspCmd.cmd = 30;
861         myVspCmd.sub_data.kern.token = dma_addr;
862         myVspCmd.sub_data.kern.address_type = HvLpDma_AddressType_TceIndex;
863         myVspCmd.sub_data.kern.side = side;
864         myVspCmd.sub_data.kern.offset = offset;
865         myVspCmd.sub_data.kern.length = size;
866         mb();
867         rc = signal_vsp_instruction(&myVspCmd);
868         if (rc == 0) {
869                 if (myVspCmd.result_code == 0)
870                         rc = 0;
871                 else
872                         rc = -ENOMEM;
873         }
874
875         dma_free_coherent(iSeries_vio_dev, size, page, dma_addr);
876
877         return rc;
878 }
879
880 int mf_getVmlinuxChunk(char *buffer, int *size, int offset, u64 side)
881 {
882         struct VspCmdData myVspCmd;
883         int rc;
884         int len = *size;
885         dma_addr_t dma_addr;
886
887         dma_addr = dma_map_single(iSeries_vio_dev, buffer, len,
888                         DMA_FROM_DEVICE);
889         memset(buffer, 0, len);
890         memset(&myVspCmd, 0, sizeof(myVspCmd));
891         myVspCmd.cmd = 32;
892         myVspCmd.sub_data.kern.token = dma_addr;
893         myVspCmd.sub_data.kern.address_type = HvLpDma_AddressType_TceIndex;
894         myVspCmd.sub_data.kern.side = side;
895         myVspCmd.sub_data.kern.offset = offset;
896         myVspCmd.sub_data.kern.length = len;
897         mb();
898         rc = signal_vsp_instruction(&myVspCmd);
899         if (rc == 0) {
900                 if (myVspCmd.result_code == 0)
901                         *size = myVspCmd.sub_data.length_out;
902                 else
903                         rc = -ENOMEM;
904         }
905
906         dma_unmap_single(iSeries_vio_dev, dma_addr, len, DMA_FROM_DEVICE);
907
908         return rc;
909 }
910
911 int mf_setRtcTime(unsigned long time)
912 {
913         struct rtc_time tm;
914
915         to_tm(time, &tm);
916
917         return mf_setRtc(&tm);
918 }
919
920 struct RtcTimeData {
921         struct completion com;
922         struct CeMsgData xCeMsg;
923         int xRc;
924 };
925
926 void getRtcTimeComplete(void * token, struct CeMsgData *ceMsg)
927 {
928         struct RtcTimeData *rtc = (struct RtcTimeData *)token;
929
930         memcpy(&(rtc->xCeMsg), ceMsg, sizeof(rtc->xCeMsg));
931         rtc->xRc = 0;
932         complete(&rtc->com);
933 }
934
935 static unsigned long lastsec = 1;
936
937 int mf_getRtcTime(unsigned long *time)
938 {
939         u32 dataWord1 = *((u32 *)(&xSpCommArea.xBcdTimeAtIplStart));
940         u32 dataWord2 = *(((u32 *)&(xSpCommArea.xBcdTimeAtIplStart)) + 1);
941         int year = 1970;
942         int year1 = (dataWord1 >> 24) & 0x000000FF;
943         int year2 = (dataWord1 >> 16) & 0x000000FF;
944         int sec = (dataWord1 >> 8) & 0x000000FF;
945         int min = dataWord1 & 0x000000FF;
946         int hour = (dataWord2 >> 24) & 0x000000FF;
947         int day = (dataWord2 >> 8) & 0x000000FF;
948         int mon = dataWord2 & 0x000000FF;
949
950         BCD_TO_BIN(sec);
951         BCD_TO_BIN(min);
952         BCD_TO_BIN(hour);
953         BCD_TO_BIN(day);
954         BCD_TO_BIN(mon);
955         BCD_TO_BIN(year1);
956         BCD_TO_BIN(year2);
957         year = year1 * 100 + year2;
958
959         *time = mktime(year, mon, day, hour, min, sec);
960         *time += (jiffies / HZ);
961     
962         /*
963          * Now THIS is a nasty hack!
964          * It ensures that the first two calls to mf_getRtcTime get different
965          * answers.  That way the loop in init_time (time.c) will not think
966          * the clock is stuck.
967          */
968         if (lastsec) {
969                 *time -= lastsec;
970                 --lastsec;
971         }
972         return 0;
973 }
974
975 int mf_getRtc(struct rtc_time *tm)
976 {
977         struct CeMsgCompleteData ceComplete;
978         struct RtcTimeData rtcData;
979         int rc;
980
981         memset(&ceComplete, 0, sizeof(ceComplete));
982         memset(&rtcData, 0, sizeof(rtcData));
983         init_completion(&rtcData.com);
984         ceComplete.handler = &getRtcTimeComplete;
985         ceComplete.token = (void *)&rtcData;
986         rc = signal_ce_msg("\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\x00",
987                         &ceComplete);
988         if (rc == 0) {
989                 wait_for_completion(&rtcData.com);
990
991                 if (rtcData.xRc == 0) {
992                         if ((rtcData.xCeMsg.ce_msg[2] == 0xa9) ||
993                             (rtcData.xCeMsg.ce_msg[2] == 0xaf)) {
994                                 /* TOD clock is not set */
995                                 tm->tm_sec = 1;
996                                 tm->tm_min = 1;
997                                 tm->tm_hour = 1;
998                                 tm->tm_mday = 10;
999                                 tm->tm_mon = 8;
1000                                 tm->tm_year = 71;
1001                                 mf_setRtc(tm);
1002                         }
1003                         {
1004                                 u32 dataWord1 = *((u32 *)(rtcData.xCeMsg.ce_msg+4));
1005                                 u32 dataWord2 = *((u32 *)(rtcData.xCeMsg.ce_msg+8));
1006                                 u8 year = (dataWord1 >> 16) & 0x000000FF;
1007                                 u8 sec = (dataWord1 >> 8) & 0x000000FF;
1008                                 u8 min = dataWord1 & 0x000000FF;
1009                                 u8 hour = (dataWord2 >> 24) & 0x000000FF;
1010                                 u8 day = (dataWord2 >> 8) & 0x000000FF;
1011                                 u8 mon = dataWord2 & 0x000000FF;
1012
1013                                 BCD_TO_BIN(sec);
1014                                 BCD_TO_BIN(min);
1015                                 BCD_TO_BIN(hour);
1016                                 BCD_TO_BIN(day);
1017                                 BCD_TO_BIN(mon);
1018                                 BCD_TO_BIN(year);
1019
1020                                 if (year <= 69)
1021                                         year += 100;
1022             
1023                                 tm->tm_sec = sec;
1024                                 tm->tm_min = min;
1025                                 tm->tm_hour = hour;
1026                                 tm->tm_mday = day;
1027                                 tm->tm_mon = mon;
1028                                 tm->tm_year = year;
1029                         }
1030                 } else {
1031                         rc = rtcData.xRc;
1032                         tm->tm_sec = 0;
1033                         tm->tm_min = 0;
1034                         tm->tm_hour = 0;
1035                         tm->tm_mday = 15;
1036                         tm->tm_mon = 5;
1037                         tm->tm_year = 52;
1038
1039                 }
1040                 tm->tm_wday = 0;
1041                 tm->tm_yday = 0;
1042                 tm->tm_isdst = 0;
1043         }
1044
1045         return rc;
1046 }
1047
1048 int mf_setRtc(struct rtc_time * tm)
1049 {
1050         char ceTime[12] = "\x00\x00\x00\x41\x00\x00\x00\x00\x00\x00\x00\x00";
1051         u8 day, mon, hour, min, sec, y1, y2;
1052         unsigned year;
1053     
1054         year = 1900 + tm->tm_year;
1055         y1 = year / 100;
1056         y2 = year % 100;
1057     
1058         sec = tm->tm_sec;
1059         min = tm->tm_min;
1060         hour = tm->tm_hour;
1061         day = tm->tm_mday;
1062         mon = tm->tm_mon + 1;
1063             
1064         BIN_TO_BCD(sec);
1065         BIN_TO_BCD(min);
1066         BIN_TO_BCD(hour);
1067         BIN_TO_BCD(mon);
1068         BIN_TO_BCD(day);
1069         BIN_TO_BCD(y1);
1070         BIN_TO_BCD(y2);
1071
1072         ceTime[4] = y1;
1073         ceTime[5] = y2;
1074         ceTime[6] = sec;
1075         ceTime[7] = min;
1076         ceTime[8] = hour;
1077         ceTime[10] = day;
1078         ceTime[11] = mon;
1079    
1080         return signal_ce_msg(ceTime, NULL);
1081 }