ds.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. /* ds.c: Domain Services driver for Logical Domains
  2. *
  3. * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/types.h>
  8. #include <linux/string.h>
  9. #include <linux/slab.h>
  10. #include <linux/sched.h>
  11. #include <linux/delay.h>
  12. #include <linux/mutex.h>
  13. #include <linux/kthread.h>
  14. #include <linux/reboot.h>
  15. #include <linux/cpu.h>
  16. #include <asm/hypervisor.h>
  17. #include <asm/ldc.h>
  18. #include <asm/vio.h>
  19. #include <asm/mdesc.h>
  20. #include <asm/head.h>
  21. #include <asm/irq.h>
  22. #include "kernel.h"
  23. #define DRV_MODULE_NAME "ds"
  24. #define PFX DRV_MODULE_NAME ": "
  25. #define DRV_MODULE_VERSION "1.0"
  26. #define DRV_MODULE_RELDATE "Jul 11, 2007"
  27. static char version[] =
  28. DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
  29. MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
  30. MODULE_DESCRIPTION("Sun LDOM domain services driver");
  31. MODULE_LICENSE("GPL");
  32. MODULE_VERSION(DRV_MODULE_VERSION);
  33. struct ds_msg_tag {
  34. __u32 type;
  35. #define DS_INIT_REQ 0x00
  36. #define DS_INIT_ACK 0x01
  37. #define DS_INIT_NACK 0x02
  38. #define DS_REG_REQ 0x03
  39. #define DS_REG_ACK 0x04
  40. #define DS_REG_NACK 0x05
  41. #define DS_UNREG_REQ 0x06
  42. #define DS_UNREG_ACK 0x07
  43. #define DS_UNREG_NACK 0x08
  44. #define DS_DATA 0x09
  45. #define DS_NACK 0x0a
  46. __u32 len;
  47. };
  48. /* Result codes */
  49. #define DS_OK 0x00
  50. #define DS_REG_VER_NACK 0x01
  51. #define DS_REG_DUP 0x02
  52. #define DS_INV_HDL 0x03
  53. #define DS_TYPE_UNKNOWN 0x04
  54. struct ds_version {
  55. __u16 major;
  56. __u16 minor;
  57. };
  58. struct ds_ver_req {
  59. struct ds_msg_tag tag;
  60. struct ds_version ver;
  61. };
  62. struct ds_ver_ack {
  63. struct ds_msg_tag tag;
  64. __u16 minor;
  65. };
  66. struct ds_ver_nack {
  67. struct ds_msg_tag tag;
  68. __u16 major;
  69. };
  70. struct ds_reg_req {
  71. struct ds_msg_tag tag;
  72. __u64 handle;
  73. __u16 major;
  74. __u16 minor;
  75. char svc_id[0];
  76. };
  77. struct ds_reg_ack {
  78. struct ds_msg_tag tag;
  79. __u64 handle;
  80. __u16 minor;
  81. };
  82. struct ds_reg_nack {
  83. struct ds_msg_tag tag;
  84. __u64 handle;
  85. __u16 major;
  86. };
  87. struct ds_unreg_req {
  88. struct ds_msg_tag tag;
  89. __u64 handle;
  90. };
  91. struct ds_unreg_ack {
  92. struct ds_msg_tag tag;
  93. __u64 handle;
  94. };
  95. struct ds_unreg_nack {
  96. struct ds_msg_tag tag;
  97. __u64 handle;
  98. };
  99. struct ds_data {
  100. struct ds_msg_tag tag;
  101. __u64 handle;
  102. };
  103. struct ds_data_nack {
  104. struct ds_msg_tag tag;
  105. __u64 handle;
  106. __u64 result;
  107. };
  108. struct ds_info;
  109. struct ds_cap_state {
  110. __u64 handle;
  111. void (*data)(struct ds_info *dp,
  112. struct ds_cap_state *cp,
  113. void *buf, int len);
  114. const char *service_id;
  115. u8 state;
  116. #define CAP_STATE_UNKNOWN 0x00
  117. #define CAP_STATE_REG_SENT 0x01
  118. #define CAP_STATE_REGISTERED 0x02
  119. };
  120. static void md_update_data(struct ds_info *dp, struct ds_cap_state *cp,
  121. void *buf, int len);
  122. static void domain_shutdown_data(struct ds_info *dp,
  123. struct ds_cap_state *cp,
  124. void *buf, int len);
  125. static void domain_panic_data(struct ds_info *dp,
  126. struct ds_cap_state *cp,
  127. void *buf, int len);
  128. #ifdef CONFIG_HOTPLUG_CPU
  129. static void dr_cpu_data(struct ds_info *dp,
  130. struct ds_cap_state *cp,
  131. void *buf, int len);
  132. #endif
  133. static void ds_pri_data(struct ds_info *dp,
  134. struct ds_cap_state *cp,
  135. void *buf, int len);
  136. static void ds_var_data(struct ds_info *dp,
  137. struct ds_cap_state *cp,
  138. void *buf, int len);
  139. static struct ds_cap_state ds_states_template[] = {
  140. {
  141. .service_id = "md-update",
  142. .data = md_update_data,
  143. },
  144. {
  145. .service_id = "domain-shutdown",
  146. .data = domain_shutdown_data,
  147. },
  148. {
  149. .service_id = "domain-panic",
  150. .data = domain_panic_data,
  151. },
  152. #ifdef CONFIG_HOTPLUG_CPU
  153. {
  154. .service_id = "dr-cpu",
  155. .data = dr_cpu_data,
  156. },
  157. #endif
  158. {
  159. .service_id = "pri",
  160. .data = ds_pri_data,
  161. },
  162. {
  163. .service_id = "var-config",
  164. .data = ds_var_data,
  165. },
  166. {
  167. .service_id = "var-config-backup",
  168. .data = ds_var_data,
  169. },
  170. };
  171. static DEFINE_SPINLOCK(ds_lock);
  172. struct ds_info {
  173. struct ldc_channel *lp;
  174. u8 hs_state;
  175. #define DS_HS_START 0x01
  176. #define DS_HS_DONE 0x02
  177. u64 id;
  178. void *rcv_buf;
  179. int rcv_buf_len;
  180. struct ds_cap_state *ds_states;
  181. int num_ds_states;
  182. struct ds_info *next;
  183. };
  184. static struct ds_info *ds_info_list;
  185. static struct ds_cap_state *find_cap(struct ds_info *dp, u64 handle)
  186. {
  187. unsigned int index = handle >> 32;
  188. if (index >= dp->num_ds_states)
  189. return NULL;
  190. return &dp->ds_states[index];
  191. }
  192. static struct ds_cap_state *find_cap_by_string(struct ds_info *dp,
  193. const char *name)
  194. {
  195. int i;
  196. for (i = 0; i < dp->num_ds_states; i++) {
  197. if (strcmp(dp->ds_states[i].service_id, name))
  198. continue;
  199. return &dp->ds_states[i];
  200. }
  201. return NULL;
  202. }
  203. static int __ds_send(struct ldc_channel *lp, void *data, int len)
  204. {
  205. int err, limit = 1000;
  206. err = -EINVAL;
  207. while (limit-- > 0) {
  208. err = ldc_write(lp, data, len);
  209. if (!err || (err != -EAGAIN))
  210. break;
  211. udelay(1);
  212. }
  213. return err;
  214. }
  215. static int ds_send(struct ldc_channel *lp, void *data, int len)
  216. {
  217. unsigned long flags;
  218. int err;
  219. spin_lock_irqsave(&ds_lock, flags);
  220. err = __ds_send(lp, data, len);
  221. spin_unlock_irqrestore(&ds_lock, flags);
  222. return err;
  223. }
  224. struct ds_md_update_req {
  225. __u64 req_num;
  226. };
  227. struct ds_md_update_res {
  228. __u64 req_num;
  229. __u32 result;
  230. };
  231. static void md_update_data(struct ds_info *dp,
  232. struct ds_cap_state *cp,
  233. void *buf, int len)
  234. {
  235. struct ldc_channel *lp = dp->lp;
  236. struct ds_data *dpkt = buf;
  237. struct ds_md_update_req *rp;
  238. struct {
  239. struct ds_data data;
  240. struct ds_md_update_res res;
  241. } pkt;
  242. rp = (struct ds_md_update_req *) (dpkt + 1);
  243. printk(KERN_INFO "ds-%llu: Machine description update.\n", dp->id);
  244. mdesc_update();
  245. memset(&pkt, 0, sizeof(pkt));
  246. pkt.data.tag.type = DS_DATA;
  247. pkt.data.tag.len = sizeof(pkt) - sizeof(struct ds_msg_tag);
  248. pkt.data.handle = cp->handle;
  249. pkt.res.req_num = rp->req_num;
  250. pkt.res.result = DS_OK;
  251. ds_send(lp, &pkt, sizeof(pkt));
  252. }
  253. struct ds_shutdown_req {
  254. __u64 req_num;
  255. __u32 ms_delay;
  256. };
  257. struct ds_shutdown_res {
  258. __u64 req_num;
  259. __u32 result;
  260. char reason[1];
  261. };
  262. static void domain_shutdown_data(struct ds_info *dp,
  263. struct ds_cap_state *cp,
  264. void *buf, int len)
  265. {
  266. struct ldc_channel *lp = dp->lp;
  267. struct ds_data *dpkt = buf;
  268. struct ds_shutdown_req *rp;
  269. struct {
  270. struct ds_data data;
  271. struct ds_shutdown_res res;
  272. } pkt;
  273. rp = (struct ds_shutdown_req *) (dpkt + 1);
  274. printk(KERN_ALERT "ds-%llu: Shutdown request from "
  275. "LDOM manager received.\n", dp->id);
  276. memset(&pkt, 0, sizeof(pkt));
  277. pkt.data.tag.type = DS_DATA;
  278. pkt.data.tag.len = sizeof(pkt) - sizeof(struct ds_msg_tag);
  279. pkt.data.handle = cp->handle;
  280. pkt.res.req_num = rp->req_num;
  281. pkt.res.result = DS_OK;
  282. pkt.res.reason[0] = 0;
  283. ds_send(lp, &pkt, sizeof(pkt));
  284. orderly_poweroff(true);
  285. }
  286. struct ds_panic_req {
  287. __u64 req_num;
  288. };
  289. struct ds_panic_res {
  290. __u64 req_num;
  291. __u32 result;
  292. char reason[1];
  293. };
  294. static void domain_panic_data(struct ds_info *dp,
  295. struct ds_cap_state *cp,
  296. void *buf, int len)
  297. {
  298. struct ldc_channel *lp = dp->lp;
  299. struct ds_data *dpkt = buf;
  300. struct ds_panic_req *rp;
  301. struct {
  302. struct ds_data data;
  303. struct ds_panic_res res;
  304. } pkt;
  305. rp = (struct ds_panic_req *) (dpkt + 1);
  306. printk(KERN_ALERT "ds-%llu: Panic request from "
  307. "LDOM manager received.\n", dp->id);
  308. memset(&pkt, 0, sizeof(pkt));
  309. pkt.data.tag.type = DS_DATA;
  310. pkt.data.tag.len = sizeof(pkt) - sizeof(struct ds_msg_tag);
  311. pkt.data.handle = cp->handle;
  312. pkt.res.req_num = rp->req_num;
  313. pkt.res.result = DS_OK;
  314. pkt.res.reason[0] = 0;
  315. ds_send(lp, &pkt, sizeof(pkt));
  316. panic("PANIC requested by LDOM manager.");
  317. }
  318. #ifdef CONFIG_HOTPLUG_CPU
  319. struct dr_cpu_tag {
  320. __u64 req_num;
  321. __u32 type;
  322. #define DR_CPU_CONFIGURE 0x43
  323. #define DR_CPU_UNCONFIGURE 0x55
  324. #define DR_CPU_FORCE_UNCONFIGURE 0x46
  325. #define DR_CPU_STATUS 0x53
  326. /* Responses */
  327. #define DR_CPU_OK 0x6f
  328. #define DR_CPU_ERROR 0x65
  329. __u32 num_records;
  330. };
  331. struct dr_cpu_resp_entry {
  332. __u32 cpu;
  333. __u32 result;
  334. #define DR_CPU_RES_OK 0x00
  335. #define DR_CPU_RES_FAILURE 0x01
  336. #define DR_CPU_RES_BLOCKED 0x02
  337. #define DR_CPU_RES_CPU_NOT_RESPONDING 0x03
  338. #define DR_CPU_RES_NOT_IN_MD 0x04
  339. __u32 stat;
  340. #define DR_CPU_STAT_NOT_PRESENT 0x00
  341. #define DR_CPU_STAT_UNCONFIGURED 0x01
  342. #define DR_CPU_STAT_CONFIGURED 0x02
  343. __u32 str_off;
  344. };
  345. static void __dr_cpu_send_error(struct ds_info *dp,
  346. struct ds_cap_state *cp,
  347. struct ds_data *data)
  348. {
  349. struct dr_cpu_tag *tag = (struct dr_cpu_tag *) (data + 1);
  350. struct {
  351. struct ds_data data;
  352. struct dr_cpu_tag tag;
  353. } pkt;
  354. int msg_len;
  355. memset(&pkt, 0, sizeof(pkt));
  356. pkt.data.tag.type = DS_DATA;
  357. pkt.data.handle = cp->handle;
  358. pkt.tag.req_num = tag->req_num;
  359. pkt.tag.type = DR_CPU_ERROR;
  360. pkt.tag.num_records = 0;
  361. msg_len = (sizeof(struct ds_data) +
  362. sizeof(struct dr_cpu_tag));
  363. pkt.data.tag.len = msg_len - sizeof(struct ds_msg_tag);
  364. __ds_send(dp->lp, &pkt, msg_len);
  365. }
  366. static void dr_cpu_send_error(struct ds_info *dp,
  367. struct ds_cap_state *cp,
  368. struct ds_data *data)
  369. {
  370. unsigned long flags;
  371. spin_lock_irqsave(&ds_lock, flags);
  372. __dr_cpu_send_error(dp, cp, data);
  373. spin_unlock_irqrestore(&ds_lock, flags);
  374. }
  375. #define CPU_SENTINEL 0xffffffff
  376. static void purge_dups(u32 *list, u32 num_ents)
  377. {
  378. unsigned int i;
  379. for (i = 0; i < num_ents; i++) {
  380. u32 cpu = list[i];
  381. unsigned int j;
  382. if (cpu == CPU_SENTINEL)
  383. continue;
  384. for (j = i + 1; j < num_ents; j++) {
  385. if (list[j] == cpu)
  386. list[j] = CPU_SENTINEL;
  387. }
  388. }
  389. }
  390. static int dr_cpu_size_response(int ncpus)
  391. {
  392. return (sizeof(struct ds_data) +
  393. sizeof(struct dr_cpu_tag) +
  394. (sizeof(struct dr_cpu_resp_entry) * ncpus));
  395. }
  396. static void dr_cpu_init_response(struct ds_data *resp, u64 req_num,
  397. u64 handle, int resp_len, int ncpus,
  398. cpumask_t *mask, u32 default_stat)
  399. {
  400. struct dr_cpu_resp_entry *ent;
  401. struct dr_cpu_tag *tag;
  402. int i, cpu;
  403. tag = (struct dr_cpu_tag *) (resp + 1);
  404. ent = (struct dr_cpu_resp_entry *) (tag + 1);
  405. resp->tag.type = DS_DATA;
  406. resp->tag.len = resp_len - sizeof(struct ds_msg_tag);
  407. resp->handle = handle;
  408. tag->req_num = req_num;
  409. tag->type = DR_CPU_OK;
  410. tag->num_records = ncpus;
  411. i = 0;
  412. for_each_cpu(cpu, mask) {
  413. ent[i].cpu = cpu;
  414. ent[i].result = DR_CPU_RES_OK;
  415. ent[i].stat = default_stat;
  416. i++;
  417. }
  418. BUG_ON(i != ncpus);
  419. }
  420. static void dr_cpu_mark(struct ds_data *resp, int cpu, int ncpus,
  421. u32 res, u32 stat)
  422. {
  423. struct dr_cpu_resp_entry *ent;
  424. struct dr_cpu_tag *tag;
  425. int i;
  426. tag = (struct dr_cpu_tag *) (resp + 1);
  427. ent = (struct dr_cpu_resp_entry *) (tag + 1);
  428. for (i = 0; i < ncpus; i++) {
  429. if (ent[i].cpu != cpu)
  430. continue;
  431. ent[i].result = res;
  432. ent[i].stat = stat;
  433. break;
  434. }
  435. }
  436. static int dr_cpu_configure(struct ds_info *dp, struct ds_cap_state *cp,
  437. u64 req_num, cpumask_t *mask)
  438. {
  439. struct ds_data *resp;
  440. int resp_len, ncpus, cpu;
  441. unsigned long flags;
  442. ncpus = cpumask_weight(mask);
  443. resp_len = dr_cpu_size_response(ncpus);
  444. resp = kzalloc(resp_len, GFP_KERNEL);
  445. if (!resp)
  446. return -ENOMEM;
  447. dr_cpu_init_response(resp, req_num, cp->handle,
  448. resp_len, ncpus, mask,
  449. DR_CPU_STAT_CONFIGURED);
  450. mdesc_populate_present_mask(mask);
  451. mdesc_fill_in_cpu_data(mask);
  452. for_each_cpu(cpu, mask) {
  453. int err;
  454. printk(KERN_INFO "ds-%llu: Starting cpu %d...\n",
  455. dp->id, cpu);
  456. err = cpu_up(cpu);
  457. if (err) {
  458. __u32 res = DR_CPU_RES_FAILURE;
  459. __u32 stat = DR_CPU_STAT_UNCONFIGURED;
  460. if (!cpu_present(cpu)) {
  461. /* CPU not present in MD */
  462. res = DR_CPU_RES_NOT_IN_MD;
  463. stat = DR_CPU_STAT_NOT_PRESENT;
  464. } else if (err == -ENODEV) {
  465. /* CPU did not call in successfully */
  466. res = DR_CPU_RES_CPU_NOT_RESPONDING;
  467. }
  468. printk(KERN_INFO "ds-%llu: CPU startup failed err=%d\n",
  469. dp->id, err);
  470. dr_cpu_mark(resp, cpu, ncpus, res, stat);
  471. }
  472. }
  473. spin_lock_irqsave(&ds_lock, flags);
  474. __ds_send(dp->lp, resp, resp_len);
  475. spin_unlock_irqrestore(&ds_lock, flags);
  476. kfree(resp);
  477. /* Redistribute IRQs, taking into account the new cpus. */
  478. fixup_irqs();
  479. return 0;
  480. }
  481. static int dr_cpu_unconfigure(struct ds_info *dp,
  482. struct ds_cap_state *cp,
  483. u64 req_num,
  484. cpumask_t *mask)
  485. {
  486. struct ds_data *resp;
  487. int resp_len, ncpus, cpu;
  488. unsigned long flags;
  489. ncpus = cpumask_weight(mask);
  490. resp_len = dr_cpu_size_response(ncpus);
  491. resp = kzalloc(resp_len, GFP_KERNEL);
  492. if (!resp)
  493. return -ENOMEM;
  494. dr_cpu_init_response(resp, req_num, cp->handle,
  495. resp_len, ncpus, mask,
  496. DR_CPU_STAT_UNCONFIGURED);
  497. for_each_cpu(cpu, mask) {
  498. int err;
  499. printk(KERN_INFO "ds-%llu: Shutting down cpu %d...\n",
  500. dp->id, cpu);
  501. err = cpu_down(cpu);
  502. if (err)
  503. dr_cpu_mark(resp, cpu, ncpus,
  504. DR_CPU_RES_FAILURE,
  505. DR_CPU_STAT_CONFIGURED);
  506. }
  507. spin_lock_irqsave(&ds_lock, flags);
  508. __ds_send(dp->lp, resp, resp_len);
  509. spin_unlock_irqrestore(&ds_lock, flags);
  510. kfree(resp);
  511. return 0;
  512. }
  513. static void dr_cpu_data(struct ds_info *dp, struct ds_cap_state *cp, void *buf,
  514. int len)
  515. {
  516. struct ds_data *data = buf;
  517. struct dr_cpu_tag *tag = (struct dr_cpu_tag *) (data + 1);
  518. u32 *cpu_list = (u32 *) (tag + 1);
  519. u64 req_num = tag->req_num;
  520. cpumask_t mask;
  521. unsigned int i;
  522. int err;
  523. switch (tag->type) {
  524. case DR_CPU_CONFIGURE:
  525. case DR_CPU_UNCONFIGURE:
  526. case DR_CPU_FORCE_UNCONFIGURE:
  527. break;
  528. default:
  529. dr_cpu_send_error(dp, cp, data);
  530. return;
  531. }
  532. purge_dups(cpu_list, tag->num_records);
  533. cpumask_clear(&mask);
  534. for (i = 0; i < tag->num_records; i++) {
  535. if (cpu_list[i] == CPU_SENTINEL)
  536. continue;
  537. if (cpu_list[i] < nr_cpu_ids)
  538. cpumask_set_cpu(cpu_list[i], &mask);
  539. }
  540. if (tag->type == DR_CPU_CONFIGURE)
  541. err = dr_cpu_configure(dp, cp, req_num, &mask);
  542. else
  543. err = dr_cpu_unconfigure(dp, cp, req_num, &mask);
  544. if (err)
  545. dr_cpu_send_error(dp, cp, data);
  546. }
  547. #endif /* CONFIG_HOTPLUG_CPU */
  548. struct ds_pri_msg {
  549. __u64 req_num;
  550. __u64 type;
  551. #define DS_PRI_REQUEST 0x00
  552. #define DS_PRI_DATA 0x01
  553. #define DS_PRI_UPDATE 0x02
  554. };
  555. static void ds_pri_data(struct ds_info *dp,
  556. struct ds_cap_state *cp,
  557. void *buf, int len)
  558. {
  559. struct ds_data *dpkt = buf;
  560. struct ds_pri_msg *rp;
  561. rp = (struct ds_pri_msg *) (dpkt + 1);
  562. printk(KERN_INFO "ds-%llu: PRI REQ [%llx:%llx], len=%d\n",
  563. dp->id, rp->req_num, rp->type, len);
  564. }
  565. struct ds_var_hdr {
  566. __u32 type;
  567. #define DS_VAR_SET_REQ 0x00
  568. #define DS_VAR_DELETE_REQ 0x01
  569. #define DS_VAR_SET_RESP 0x02
  570. #define DS_VAR_DELETE_RESP 0x03
  571. };
  572. struct ds_var_set_msg {
  573. struct ds_var_hdr hdr;
  574. char name_and_value[0];
  575. };
  576. struct ds_var_delete_msg {
  577. struct ds_var_hdr hdr;
  578. char name[0];
  579. };
  580. struct ds_var_resp {
  581. struct ds_var_hdr hdr;
  582. __u32 result;
  583. #define DS_VAR_SUCCESS 0x00
  584. #define DS_VAR_NO_SPACE 0x01
  585. #define DS_VAR_INVALID_VAR 0x02
  586. #define DS_VAR_INVALID_VAL 0x03
  587. #define DS_VAR_NOT_PRESENT 0x04
  588. };
  589. static DEFINE_MUTEX(ds_var_mutex);
  590. static int ds_var_doorbell;
  591. static int ds_var_response;
  592. static void ds_var_data(struct ds_info *dp,
  593. struct ds_cap_state *cp,
  594. void *buf, int len)
  595. {
  596. struct ds_data *dpkt = buf;
  597. struct ds_var_resp *rp;
  598. rp = (struct ds_var_resp *) (dpkt + 1);
  599. if (rp->hdr.type != DS_VAR_SET_RESP &&
  600. rp->hdr.type != DS_VAR_DELETE_RESP)
  601. return;
  602. ds_var_response = rp->result;
  603. wmb();
  604. ds_var_doorbell = 1;
  605. }
  606. void ldom_set_var(const char *var, const char *value)
  607. {
  608. struct ds_cap_state *cp;
  609. struct ds_info *dp;
  610. unsigned long flags;
  611. spin_lock_irqsave(&ds_lock, flags);
  612. cp = NULL;
  613. for (dp = ds_info_list; dp; dp = dp->next) {
  614. struct ds_cap_state *tmp;
  615. tmp = find_cap_by_string(dp, "var-config");
  616. if (tmp && tmp->state == CAP_STATE_REGISTERED) {
  617. cp = tmp;
  618. break;
  619. }
  620. }
  621. if (!cp) {
  622. for (dp = ds_info_list; dp; dp = dp->next) {
  623. struct ds_cap_state *tmp;
  624. tmp = find_cap_by_string(dp, "var-config-backup");
  625. if (tmp && tmp->state == CAP_STATE_REGISTERED) {
  626. cp = tmp;
  627. break;
  628. }
  629. }
  630. }
  631. spin_unlock_irqrestore(&ds_lock, flags);
  632. if (cp) {
  633. union {
  634. struct {
  635. struct ds_data data;
  636. struct ds_var_set_msg msg;
  637. } header;
  638. char all[512];
  639. } pkt;
  640. char *base, *p;
  641. int msg_len, loops;
  642. if (strlen(var) + strlen(value) + 2 >
  643. sizeof(pkt) - sizeof(pkt.header)) {
  644. printk(KERN_ERR PFX
  645. "contents length: %zu, which more than max: %lu,"
  646. "so could not set (%s) variable to (%s).\n",
  647. strlen(var) + strlen(value) + 2,
  648. sizeof(pkt) - sizeof(pkt.header), var, value);
  649. return;
  650. }
  651. memset(&pkt, 0, sizeof(pkt));
  652. pkt.header.data.tag.type = DS_DATA;
  653. pkt.header.data.handle = cp->handle;
  654. pkt.header.msg.hdr.type = DS_VAR_SET_REQ;
  655. base = p = &pkt.header.msg.name_and_value[0];
  656. strcpy(p, var);
  657. p += strlen(var) + 1;
  658. strcpy(p, value);
  659. p += strlen(value) + 1;
  660. msg_len = (sizeof(struct ds_data) +
  661. sizeof(struct ds_var_set_msg) +
  662. (p - base));
  663. msg_len = (msg_len + 3) & ~3;
  664. pkt.header.data.tag.len = msg_len - sizeof(struct ds_msg_tag);
  665. mutex_lock(&ds_var_mutex);
  666. spin_lock_irqsave(&ds_lock, flags);
  667. ds_var_doorbell = 0;
  668. ds_var_response = -1;
  669. __ds_send(dp->lp, &pkt, msg_len);
  670. spin_unlock_irqrestore(&ds_lock, flags);
  671. loops = 1000;
  672. while (ds_var_doorbell == 0) {
  673. if (loops-- < 0)
  674. break;
  675. barrier();
  676. udelay(100);
  677. }
  678. mutex_unlock(&ds_var_mutex);
  679. if (ds_var_doorbell == 0 ||
  680. ds_var_response != DS_VAR_SUCCESS)
  681. printk(KERN_ERR "ds-%llu: var-config [%s:%s] "
  682. "failed, response(%d).\n",
  683. dp->id, var, value,
  684. ds_var_response);
  685. } else {
  686. printk(KERN_ERR PFX "var-config not registered so "
  687. "could not set (%s) variable to (%s).\n",
  688. var, value);
  689. }
  690. }
  691. static char full_boot_str[256] __attribute__((aligned(32)));
  692. static int reboot_data_supported;
  693. void ldom_reboot(const char *boot_command)
  694. {
  695. /* Don't bother with any of this if the boot_command
  696. * is empty.
  697. */
  698. if (boot_command && strlen(boot_command)) {
  699. unsigned long len;
  700. snprintf(full_boot_str, sizeof(full_boot_str), "boot %s",
  701. boot_command);
  702. len = strlen(full_boot_str);
  703. if (reboot_data_supported) {
  704. unsigned long ra = kimage_addr_to_ra(full_boot_str);
  705. unsigned long hv_ret;
  706. hv_ret = sun4v_reboot_data_set(ra, len);
  707. if (hv_ret != HV_EOK)
  708. pr_err("SUN4V: Unable to set reboot data "
  709. "hv_ret=%lu\n", hv_ret);
  710. } else {
  711. ldom_set_var("reboot-command", full_boot_str);
  712. }
  713. }
  714. sun4v_mach_sir();
  715. }
  716. void ldom_power_off(void)
  717. {
  718. sun4v_mach_exit(0);
  719. }
  720. static void ds_conn_reset(struct ds_info *dp)
  721. {
  722. printk(KERN_ERR "ds-%llu: ds_conn_reset() from %pf\n",
  723. dp->id, __builtin_return_address(0));
  724. }
  725. static int register_services(struct ds_info *dp)
  726. {
  727. struct ldc_channel *lp = dp->lp;
  728. int i;
  729. for (i = 0; i < dp->num_ds_states; i++) {
  730. struct {
  731. struct ds_reg_req req;
  732. u8 id_buf[256];
  733. } pbuf;
  734. struct ds_cap_state *cp = &dp->ds_states[i];
  735. int err, msg_len;
  736. u64 new_count;
  737. if (cp->state == CAP_STATE_REGISTERED)
  738. continue;
  739. new_count = sched_clock() & 0xffffffff;
  740. cp->handle = ((u64) i << 32) | new_count;
  741. msg_len = (sizeof(struct ds_reg_req) +
  742. strlen(cp->service_id));
  743. memset(&pbuf, 0, sizeof(pbuf));
  744. pbuf.req.tag.type = DS_REG_REQ;
  745. pbuf.req.tag.len = (msg_len - sizeof(struct ds_msg_tag));
  746. pbuf.req.handle = cp->handle;
  747. pbuf.req.major = 1;
  748. pbuf.req.minor = 0;
  749. strcpy(pbuf.id_buf, cp->service_id);
  750. err = __ds_send(lp, &pbuf, msg_len);
  751. if (err > 0)
  752. cp->state = CAP_STATE_REG_SENT;
  753. }
  754. return 0;
  755. }
  756. static int ds_handshake(struct ds_info *dp, struct ds_msg_tag *pkt)
  757. {
  758. if (dp->hs_state == DS_HS_START) {
  759. if (pkt->type != DS_INIT_ACK)
  760. goto conn_reset;
  761. dp->hs_state = DS_HS_DONE;
  762. return register_services(dp);
  763. }
  764. if (dp->hs_state != DS_HS_DONE)
  765. goto conn_reset;
  766. if (pkt->type == DS_REG_ACK) {
  767. struct ds_reg_ack *ap = (struct ds_reg_ack *) pkt;
  768. struct ds_cap_state *cp = find_cap(dp, ap->handle);
  769. if (!cp) {
  770. printk(KERN_ERR "ds-%llu: REG ACK for unknown "
  771. "handle %llx\n", dp->id, ap->handle);
  772. return 0;
  773. }
  774. printk(KERN_INFO "ds-%llu: Registered %s service.\n",
  775. dp->id, cp->service_id);
  776. cp->state = CAP_STATE_REGISTERED;
  777. } else if (pkt->type == DS_REG_NACK) {
  778. struct ds_reg_nack *np = (struct ds_reg_nack *) pkt;
  779. struct ds_cap_state *cp = find_cap(dp, np->handle);
  780. if (!cp) {
  781. printk(KERN_ERR "ds-%llu: REG NACK for "
  782. "unknown handle %llx\n",
  783. dp->id, np->handle);
  784. return 0;
  785. }
  786. cp->state = CAP_STATE_UNKNOWN;
  787. }
  788. return 0;
  789. conn_reset:
  790. ds_conn_reset(dp);
  791. return -ECONNRESET;
  792. }
  793. static void __send_ds_nack(struct ds_info *dp, u64 handle)
  794. {
  795. struct ds_data_nack nack = {
  796. .tag = {
  797. .type = DS_NACK,
  798. .len = (sizeof(struct ds_data_nack) -
  799. sizeof(struct ds_msg_tag)),
  800. },
  801. .handle = handle,
  802. .result = DS_INV_HDL,
  803. };
  804. __ds_send(dp->lp, &nack, sizeof(nack));
  805. }
  806. static LIST_HEAD(ds_work_list);
  807. static DECLARE_WAIT_QUEUE_HEAD(ds_wait);
  808. struct ds_queue_entry {
  809. struct list_head list;
  810. struct ds_info *dp;
  811. int req_len;
  812. int __pad;
  813. u64 req[0];
  814. };
  815. static void process_ds_work(void)
  816. {
  817. struct ds_queue_entry *qp, *tmp;
  818. unsigned long flags;
  819. LIST_HEAD(todo);
  820. spin_lock_irqsave(&ds_lock, flags);
  821. list_splice_init(&ds_work_list, &todo);
  822. spin_unlock_irqrestore(&ds_lock, flags);
  823. list_for_each_entry_safe(qp, tmp, &todo, list) {
  824. struct ds_data *dpkt = (struct ds_data *) qp->req;
  825. struct ds_info *dp = qp->dp;
  826. struct ds_cap_state *cp = find_cap(dp, dpkt->handle);
  827. int req_len = qp->req_len;
  828. if (!cp) {
  829. printk(KERN_ERR "ds-%llu: Data for unknown "
  830. "handle %llu\n",
  831. dp->id, dpkt->handle);
  832. spin_lock_irqsave(&ds_lock, flags);
  833. __send_ds_nack(dp, dpkt->handle);
  834. spin_unlock_irqrestore(&ds_lock, flags);
  835. } else {
  836. cp->data(dp, cp, dpkt, req_len);
  837. }
  838. list_del(&qp->list);
  839. kfree(qp);
  840. }
  841. }
  842. static int ds_thread(void *__unused)
  843. {
  844. DEFINE_WAIT(wait);
  845. while (1) {
  846. prepare_to_wait(&ds_wait, &wait, TASK_INTERRUPTIBLE);
  847. if (list_empty(&ds_work_list))
  848. schedule();
  849. finish_wait(&ds_wait, &wait);
  850. if (kthread_should_stop())
  851. break;
  852. process_ds_work();
  853. }
  854. return 0;
  855. }
  856. static int ds_data(struct ds_info *dp, struct ds_msg_tag *pkt, int len)
  857. {
  858. struct ds_data *dpkt = (struct ds_data *) pkt;
  859. struct ds_queue_entry *qp;
  860. qp = kmalloc(sizeof(struct ds_queue_entry) + len, GFP_ATOMIC);
  861. if (!qp) {
  862. __send_ds_nack(dp, dpkt->handle);
  863. } else {
  864. qp->dp = dp;
  865. memcpy(&qp->req, pkt, len);
  866. list_add_tail(&qp->list, &ds_work_list);
  867. wake_up(&ds_wait);
  868. }
  869. return 0;
  870. }
  871. static void ds_up(struct ds_info *dp)
  872. {
  873. struct ldc_channel *lp = dp->lp;
  874. struct ds_ver_req req;
  875. int err;
  876. req.tag.type = DS_INIT_REQ;
  877. req.tag.len = sizeof(req) - sizeof(struct ds_msg_tag);
  878. req.ver.major = 1;
  879. req.ver.minor = 0;
  880. err = __ds_send(lp, &req, sizeof(req));
  881. if (err > 0)
  882. dp->hs_state = DS_HS_START;
  883. }
  884. static void ds_reset(struct ds_info *dp)
  885. {
  886. int i;
  887. dp->hs_state = 0;
  888. for (i = 0; i < dp->num_ds_states; i++) {
  889. struct ds_cap_state *cp = &dp->ds_states[i];
  890. cp->state = CAP_STATE_UNKNOWN;
  891. }
  892. }
  893. static void ds_event(void *arg, int event)
  894. {
  895. struct ds_info *dp = arg;
  896. struct ldc_channel *lp = dp->lp;
  897. unsigned long flags;
  898. int err;
  899. spin_lock_irqsave(&ds_lock, flags);
  900. if (event == LDC_EVENT_UP) {
  901. ds_up(dp);
  902. spin_unlock_irqrestore(&ds_lock, flags);
  903. return;
  904. }
  905. if (event == LDC_EVENT_RESET) {
  906. ds_reset(dp);
  907. spin_unlock_irqrestore(&ds_lock, flags);
  908. return;
  909. }
  910. if (event != LDC_EVENT_DATA_READY) {
  911. printk(KERN_WARNING "ds-%llu: Unexpected LDC event %d\n",
  912. dp->id, event);
  913. spin_unlock_irqrestore(&ds_lock, flags);
  914. return;
  915. }
  916. err = 0;
  917. while (1) {
  918. struct ds_msg_tag *tag;
  919. err = ldc_read(lp, dp->rcv_buf, sizeof(*tag));
  920. if (unlikely(err < 0)) {
  921. if (err == -ECONNRESET)
  922. ds_conn_reset(dp);
  923. break;
  924. }
  925. if (err == 0)
  926. break;
  927. tag = dp->rcv_buf;
  928. err = ldc_read(lp, tag + 1, tag->len);
  929. if (unlikely(err < 0)) {
  930. if (err == -ECONNRESET)
  931. ds_conn_reset(dp);
  932. break;
  933. }
  934. if (err < tag->len)
  935. break;
  936. if (tag->type < DS_DATA)
  937. err = ds_handshake(dp, dp->rcv_buf);
  938. else
  939. err = ds_data(dp, dp->rcv_buf,
  940. sizeof(*tag) + err);
  941. if (err == -ECONNRESET)
  942. break;
  943. }
  944. spin_unlock_irqrestore(&ds_lock, flags);
  945. }
  946. static int ds_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  947. {
  948. static int ds_version_printed;
  949. struct ldc_channel_config ds_cfg = {
  950. .event = ds_event,
  951. .mtu = 4096,
  952. .mode = LDC_MODE_STREAM,
  953. };
  954. struct mdesc_handle *hp;
  955. struct ldc_channel *lp;
  956. struct ds_info *dp;
  957. const u64 *val;
  958. int err, i;
  959. if (ds_version_printed++ == 0)
  960. printk(KERN_INFO "%s", version);
  961. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  962. err = -ENOMEM;
  963. if (!dp)
  964. goto out_err;
  965. hp = mdesc_grab();
  966. val = mdesc_get_property(hp, vdev->mp, "id", NULL);
  967. if (val)
  968. dp->id = *val;
  969. mdesc_release(hp);
  970. dp->rcv_buf = kzalloc(4096, GFP_KERNEL);
  971. if (!dp->rcv_buf)
  972. goto out_free_dp;
  973. dp->rcv_buf_len = 4096;
  974. dp->ds_states = kmemdup(ds_states_template,
  975. sizeof(ds_states_template), GFP_KERNEL);
  976. if (!dp->ds_states)
  977. goto out_free_rcv_buf;
  978. dp->num_ds_states = ARRAY_SIZE(ds_states_template);
  979. for (i = 0; i < dp->num_ds_states; i++)
  980. dp->ds_states[i].handle = ((u64)i << 32);
  981. ds_cfg.tx_irq = vdev->tx_irq;
  982. ds_cfg.rx_irq = vdev->rx_irq;
  983. lp = ldc_alloc(vdev->channel_id, &ds_cfg, dp, "DS");
  984. if (IS_ERR(lp)) {
  985. err = PTR_ERR(lp);
  986. goto out_free_ds_states;
  987. }
  988. dp->lp = lp;
  989. err = ldc_bind(lp);
  990. if (err)
  991. goto out_free_ldc;
  992. spin_lock_irq(&ds_lock);
  993. dp->next = ds_info_list;
  994. ds_info_list = dp;
  995. spin_unlock_irq(&ds_lock);
  996. return err;
  997. out_free_ldc:
  998. ldc_free(dp->lp);
  999. out_free_ds_states:
  1000. kfree(dp->ds_states);
  1001. out_free_rcv_buf:
  1002. kfree(dp->rcv_buf);
  1003. out_free_dp:
  1004. kfree(dp);
  1005. out_err:
  1006. return err;
  1007. }
  1008. static int ds_remove(struct vio_dev *vdev)
  1009. {
  1010. return 0;
  1011. }
  1012. static const struct vio_device_id ds_match[] = {
  1013. {
  1014. .type = "domain-services-port",
  1015. },
  1016. {},
  1017. };
  1018. static struct vio_driver ds_driver = {
  1019. .id_table = ds_match,
  1020. .probe = ds_probe,
  1021. .remove = ds_remove,
  1022. .name = "ds",
  1023. };
  1024. static int __init ds_init(void)
  1025. {
  1026. unsigned long hv_ret, major, minor;
  1027. if (tlb_type == hypervisor) {
  1028. hv_ret = sun4v_get_version(HV_GRP_REBOOT_DATA, &major, &minor);
  1029. if (hv_ret == HV_EOK) {
  1030. pr_info("SUN4V: Reboot data supported (maj=%lu,min=%lu).\n",
  1031. major, minor);
  1032. reboot_data_supported = 1;
  1033. }
  1034. }
  1035. kthread_run(ds_thread, NULL, "kldomd");
  1036. return vio_register_driver(&ds_driver);
  1037. }
  1038. fs_initcall(ds_init);