mf.c 30 KB

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