mf.c 30 KB

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