mf.c 30 KB

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