ds.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  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[] __devinitdata =
  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 __cpuinit dr_cpu_configure(struct ds_info *dp,
  437. struct ds_cap_state *cp,
  438. u64 req_num,
  439. cpumask_t *mask)
  440. {
  441. struct ds_data *resp;
  442. int resp_len, ncpus, cpu;
  443. unsigned long flags;
  444. ncpus = cpumask_weight(mask);
  445. resp_len = dr_cpu_size_response(ncpus);
  446. resp = kzalloc(resp_len, GFP_KERNEL);
  447. if (!resp)
  448. return -ENOMEM;
  449. dr_cpu_init_response(resp, req_num, cp->handle,
  450. resp_len, ncpus, mask,
  451. DR_CPU_STAT_CONFIGURED);
  452. mdesc_populate_present_mask(mask);
  453. mdesc_fill_in_cpu_data(mask);
  454. for_each_cpu(cpu, mask) {
  455. int err;
  456. printk(KERN_INFO "ds-%llu: Starting cpu %d...\n",
  457. dp->id, cpu);
  458. err = cpu_up(cpu);
  459. if (err) {
  460. __u32 res = DR_CPU_RES_FAILURE;
  461. __u32 stat = DR_CPU_STAT_UNCONFIGURED;
  462. if (!cpu_present(cpu)) {
  463. /* CPU not present in MD */
  464. res = DR_CPU_RES_NOT_IN_MD;
  465. stat = DR_CPU_STAT_NOT_PRESENT;
  466. } else if (err == -ENODEV) {
  467. /* CPU did not call in successfully */
  468. res = DR_CPU_RES_CPU_NOT_RESPONDING;
  469. }
  470. printk(KERN_INFO "ds-%llu: CPU startup failed err=%d\n",
  471. dp->id, err);
  472. dr_cpu_mark(resp, cpu, ncpus, res, stat);
  473. }
  474. }
  475. spin_lock_irqsave(&ds_lock, flags);
  476. __ds_send(dp->lp, resp, resp_len);
  477. spin_unlock_irqrestore(&ds_lock, flags);
  478. kfree(resp);
  479. /* Redistribute IRQs, taking into account the new cpus. */
  480. fixup_irqs();
  481. return 0;
  482. }
  483. static int dr_cpu_unconfigure(struct ds_info *dp,
  484. struct ds_cap_state *cp,
  485. u64 req_num,
  486. cpumask_t *mask)
  487. {
  488. struct ds_data *resp;
  489. int resp_len, ncpus, cpu;
  490. unsigned long flags;
  491. ncpus = cpumask_weight(mask);
  492. resp_len = dr_cpu_size_response(ncpus);
  493. resp = kzalloc(resp_len, GFP_KERNEL);
  494. if (!resp)
  495. return -ENOMEM;
  496. dr_cpu_init_response(resp, req_num, cp->handle,
  497. resp_len, ncpus, mask,
  498. DR_CPU_STAT_UNCONFIGURED);
  499. for_each_cpu(cpu, mask) {
  500. int err;
  501. printk(KERN_INFO "ds-%llu: Shutting down cpu %d...\n",
  502. dp->id, cpu);
  503. err = cpu_down(cpu);
  504. if (err)
  505. dr_cpu_mark(resp, cpu, ncpus,
  506. DR_CPU_RES_FAILURE,
  507. DR_CPU_STAT_CONFIGURED);
  508. }
  509. spin_lock_irqsave(&ds_lock, flags);
  510. __ds_send(dp->lp, resp, resp_len);
  511. spin_unlock_irqrestore(&ds_lock, flags);
  512. kfree(resp);
  513. return 0;
  514. }
  515. static void __cpuinit dr_cpu_data(struct ds_info *dp,
  516. struct ds_cap_state *cp,
  517. void *buf, int len)
  518. {
  519. struct ds_data *data = buf;
  520. struct dr_cpu_tag *tag = (struct dr_cpu_tag *) (data + 1);
  521. u32 *cpu_list = (u32 *) (tag + 1);
  522. u64 req_num = tag->req_num;
  523. cpumask_t mask;
  524. unsigned int i;
  525. int err;
  526. switch (tag->type) {
  527. case DR_CPU_CONFIGURE:
  528. case DR_CPU_UNCONFIGURE:
  529. case DR_CPU_FORCE_UNCONFIGURE:
  530. break;
  531. default:
  532. dr_cpu_send_error(dp, cp, data);
  533. return;
  534. }
  535. purge_dups(cpu_list, tag->num_records);
  536. cpumask_clear(&mask);
  537. for (i = 0; i < tag->num_records; i++) {
  538. if (cpu_list[i] == CPU_SENTINEL)
  539. continue;
  540. if (cpu_list[i] < nr_cpu_ids)
  541. cpumask_set_cpu(cpu_list[i], &mask);
  542. }
  543. if (tag->type == DR_CPU_CONFIGURE)
  544. err = dr_cpu_configure(dp, cp, req_num, &mask);
  545. else
  546. err = dr_cpu_unconfigure(dp, cp, req_num, &mask);
  547. if (err)
  548. dr_cpu_send_error(dp, cp, data);
  549. }
  550. #endif /* CONFIG_HOTPLUG_CPU */
  551. struct ds_pri_msg {
  552. __u64 req_num;
  553. __u64 type;
  554. #define DS_PRI_REQUEST 0x00
  555. #define DS_PRI_DATA 0x01
  556. #define DS_PRI_UPDATE 0x02
  557. };
  558. static void ds_pri_data(struct ds_info *dp,
  559. struct ds_cap_state *cp,
  560. void *buf, int len)
  561. {
  562. struct ds_data *dpkt = buf;
  563. struct ds_pri_msg *rp;
  564. rp = (struct ds_pri_msg *) (dpkt + 1);
  565. printk(KERN_INFO "ds-%llu: PRI REQ [%llx:%llx], len=%d\n",
  566. dp->id, rp->req_num, rp->type, len);
  567. }
  568. struct ds_var_hdr {
  569. __u32 type;
  570. #define DS_VAR_SET_REQ 0x00
  571. #define DS_VAR_DELETE_REQ 0x01
  572. #define DS_VAR_SET_RESP 0x02
  573. #define DS_VAR_DELETE_RESP 0x03
  574. };
  575. struct ds_var_set_msg {
  576. struct ds_var_hdr hdr;
  577. char name_and_value[0];
  578. };
  579. struct ds_var_delete_msg {
  580. struct ds_var_hdr hdr;
  581. char name[0];
  582. };
  583. struct ds_var_resp {
  584. struct ds_var_hdr hdr;
  585. __u32 result;
  586. #define DS_VAR_SUCCESS 0x00
  587. #define DS_VAR_NO_SPACE 0x01
  588. #define DS_VAR_INVALID_VAR 0x02
  589. #define DS_VAR_INVALID_VAL 0x03
  590. #define DS_VAR_NOT_PRESENT 0x04
  591. };
  592. static DEFINE_MUTEX(ds_var_mutex);
  593. static int ds_var_doorbell;
  594. static int ds_var_response;
  595. static void ds_var_data(struct ds_info *dp,
  596. struct ds_cap_state *cp,
  597. void *buf, int len)
  598. {
  599. struct ds_data *dpkt = buf;
  600. struct ds_var_resp *rp;
  601. rp = (struct ds_var_resp *) (dpkt + 1);
  602. if (rp->hdr.type != DS_VAR_SET_RESP &&
  603. rp->hdr.type != DS_VAR_DELETE_RESP)
  604. return;
  605. ds_var_response = rp->result;
  606. wmb();
  607. ds_var_doorbell = 1;
  608. }
  609. void ldom_set_var(const char *var, const char *value)
  610. {
  611. struct ds_cap_state *cp;
  612. struct ds_info *dp;
  613. unsigned long flags;
  614. spin_lock_irqsave(&ds_lock, flags);
  615. cp = NULL;
  616. for (dp = ds_info_list; dp; dp = dp->next) {
  617. struct ds_cap_state *tmp;
  618. tmp = find_cap_by_string(dp, "var-config");
  619. if (tmp && tmp->state == CAP_STATE_REGISTERED) {
  620. cp = tmp;
  621. break;
  622. }
  623. }
  624. if (!cp) {
  625. for (dp = ds_info_list; dp; dp = dp->next) {
  626. struct ds_cap_state *tmp;
  627. tmp = find_cap_by_string(dp, "var-config-backup");
  628. if (tmp && tmp->state == CAP_STATE_REGISTERED) {
  629. cp = tmp;
  630. break;
  631. }
  632. }
  633. }
  634. spin_unlock_irqrestore(&ds_lock, flags);
  635. if (cp) {
  636. union {
  637. struct {
  638. struct ds_data data;
  639. struct ds_var_set_msg msg;
  640. } header;
  641. char all[512];
  642. } pkt;
  643. char *base, *p;
  644. int msg_len, loops;
  645. memset(&pkt, 0, sizeof(pkt));
  646. pkt.header.data.tag.type = DS_DATA;
  647. pkt.header.data.handle = cp->handle;
  648. pkt.header.msg.hdr.type = DS_VAR_SET_REQ;
  649. base = p = &pkt.header.msg.name_and_value[0];
  650. strcpy(p, var);
  651. p += strlen(var) + 1;
  652. strcpy(p, value);
  653. p += strlen(value) + 1;
  654. msg_len = (sizeof(struct ds_data) +
  655. sizeof(struct ds_var_set_msg) +
  656. (p - base));
  657. msg_len = (msg_len + 3) & ~3;
  658. pkt.header.data.tag.len = msg_len - sizeof(struct ds_msg_tag);
  659. mutex_lock(&ds_var_mutex);
  660. spin_lock_irqsave(&ds_lock, flags);
  661. ds_var_doorbell = 0;
  662. ds_var_response = -1;
  663. __ds_send(dp->lp, &pkt, msg_len);
  664. spin_unlock_irqrestore(&ds_lock, flags);
  665. loops = 1000;
  666. while (ds_var_doorbell == 0) {
  667. if (loops-- < 0)
  668. break;
  669. barrier();
  670. udelay(100);
  671. }
  672. mutex_unlock(&ds_var_mutex);
  673. if (ds_var_doorbell == 0 ||
  674. ds_var_response != DS_VAR_SUCCESS)
  675. printk(KERN_ERR "ds-%llu: var-config [%s:%s] "
  676. "failed, response(%d).\n",
  677. dp->id, var, value,
  678. ds_var_response);
  679. } else {
  680. printk(KERN_ERR PFX "var-config not registered so "
  681. "could not set (%s) variable to (%s).\n",
  682. var, value);
  683. }
  684. }
  685. static char full_boot_str[256] __attribute__((aligned(32)));
  686. static int reboot_data_supported;
  687. void ldom_reboot(const char *boot_command)
  688. {
  689. /* Don't bother with any of this if the boot_command
  690. * is empty.
  691. */
  692. if (boot_command && strlen(boot_command)) {
  693. unsigned long len;
  694. strcpy(full_boot_str, "boot ");
  695. strcpy(full_boot_str + strlen("boot "), boot_command);
  696. len = strlen(full_boot_str);
  697. if (reboot_data_supported) {
  698. unsigned long ra = kimage_addr_to_ra(full_boot_str);
  699. unsigned long hv_ret;
  700. hv_ret = sun4v_reboot_data_set(ra, len);
  701. if (hv_ret != HV_EOK)
  702. pr_err("SUN4V: Unable to set reboot data "
  703. "hv_ret=%lu\n", hv_ret);
  704. } else {
  705. ldom_set_var("reboot-command", full_boot_str);
  706. }
  707. }
  708. sun4v_mach_sir();
  709. }
  710. void ldom_power_off(void)
  711. {
  712. sun4v_mach_exit(0);
  713. }
  714. static void ds_conn_reset(struct ds_info *dp)
  715. {
  716. printk(KERN_ERR "ds-%llu: ds_conn_reset() from %p\n",
  717. dp->id, __builtin_return_address(0));
  718. }
  719. static int register_services(struct ds_info *dp)
  720. {
  721. struct ldc_channel *lp = dp->lp;
  722. int i;
  723. for (i = 0; i < dp->num_ds_states; i++) {
  724. struct {
  725. struct ds_reg_req req;
  726. u8 id_buf[256];
  727. } pbuf;
  728. struct ds_cap_state *cp = &dp->ds_states[i];
  729. int err, msg_len;
  730. u64 new_count;
  731. if (cp->state == CAP_STATE_REGISTERED)
  732. continue;
  733. new_count = sched_clock() & 0xffffffff;
  734. cp->handle = ((u64) i << 32) | new_count;
  735. msg_len = (sizeof(struct ds_reg_req) +
  736. strlen(cp->service_id));
  737. memset(&pbuf, 0, sizeof(pbuf));
  738. pbuf.req.tag.type = DS_REG_REQ;
  739. pbuf.req.tag.len = (msg_len - sizeof(struct ds_msg_tag));
  740. pbuf.req.handle = cp->handle;
  741. pbuf.req.major = 1;
  742. pbuf.req.minor = 0;
  743. strcpy(pbuf.req.svc_id, cp->service_id);
  744. err = __ds_send(lp, &pbuf, msg_len);
  745. if (err > 0)
  746. cp->state = CAP_STATE_REG_SENT;
  747. }
  748. return 0;
  749. }
  750. static int ds_handshake(struct ds_info *dp, struct ds_msg_tag *pkt)
  751. {
  752. if (dp->hs_state == DS_HS_START) {
  753. if (pkt->type != DS_INIT_ACK)
  754. goto conn_reset;
  755. dp->hs_state = DS_HS_DONE;
  756. return register_services(dp);
  757. }
  758. if (dp->hs_state != DS_HS_DONE)
  759. goto conn_reset;
  760. if (pkt->type == DS_REG_ACK) {
  761. struct ds_reg_ack *ap = (struct ds_reg_ack *) pkt;
  762. struct ds_cap_state *cp = find_cap(dp, ap->handle);
  763. if (!cp) {
  764. printk(KERN_ERR "ds-%llu: REG ACK for unknown "
  765. "handle %llx\n", dp->id, ap->handle);
  766. return 0;
  767. }
  768. printk(KERN_INFO "ds-%llu: Registered %s service.\n",
  769. dp->id, cp->service_id);
  770. cp->state = CAP_STATE_REGISTERED;
  771. } else if (pkt->type == DS_REG_NACK) {
  772. struct ds_reg_nack *np = (struct ds_reg_nack *) pkt;
  773. struct ds_cap_state *cp = find_cap(dp, np->handle);
  774. if (!cp) {
  775. printk(KERN_ERR "ds-%llu: REG NACK for "
  776. "unknown handle %llx\n",
  777. dp->id, np->handle);
  778. return 0;
  779. }
  780. cp->state = CAP_STATE_UNKNOWN;
  781. }
  782. return 0;
  783. conn_reset:
  784. ds_conn_reset(dp);
  785. return -ECONNRESET;
  786. }
  787. static void __send_ds_nack(struct ds_info *dp, u64 handle)
  788. {
  789. struct ds_data_nack nack = {
  790. .tag = {
  791. .type = DS_NACK,
  792. .len = (sizeof(struct ds_data_nack) -
  793. sizeof(struct ds_msg_tag)),
  794. },
  795. .handle = handle,
  796. .result = DS_INV_HDL,
  797. };
  798. __ds_send(dp->lp, &nack, sizeof(nack));
  799. }
  800. static LIST_HEAD(ds_work_list);
  801. static DECLARE_WAIT_QUEUE_HEAD(ds_wait);
  802. struct ds_queue_entry {
  803. struct list_head list;
  804. struct ds_info *dp;
  805. int req_len;
  806. int __pad;
  807. u64 req[0];
  808. };
  809. static void process_ds_work(void)
  810. {
  811. struct ds_queue_entry *qp, *tmp;
  812. unsigned long flags;
  813. LIST_HEAD(todo);
  814. spin_lock_irqsave(&ds_lock, flags);
  815. list_splice_init(&ds_work_list, &todo);
  816. spin_unlock_irqrestore(&ds_lock, flags);
  817. list_for_each_entry_safe(qp, tmp, &todo, list) {
  818. struct ds_data *dpkt = (struct ds_data *) qp->req;
  819. struct ds_info *dp = qp->dp;
  820. struct ds_cap_state *cp = find_cap(dp, dpkt->handle);
  821. int req_len = qp->req_len;
  822. if (!cp) {
  823. printk(KERN_ERR "ds-%llu: Data for unknown "
  824. "handle %llu\n",
  825. dp->id, dpkt->handle);
  826. spin_lock_irqsave(&ds_lock, flags);
  827. __send_ds_nack(dp, dpkt->handle);
  828. spin_unlock_irqrestore(&ds_lock, flags);
  829. } else {
  830. cp->data(dp, cp, dpkt, req_len);
  831. }
  832. list_del(&qp->list);
  833. kfree(qp);
  834. }
  835. }
  836. static int ds_thread(void *__unused)
  837. {
  838. DEFINE_WAIT(wait);
  839. while (1) {
  840. prepare_to_wait(&ds_wait, &wait, TASK_INTERRUPTIBLE);
  841. if (list_empty(&ds_work_list))
  842. schedule();
  843. finish_wait(&ds_wait, &wait);
  844. if (kthread_should_stop())
  845. break;
  846. process_ds_work();
  847. }
  848. return 0;
  849. }
  850. static int ds_data(struct ds_info *dp, struct ds_msg_tag *pkt, int len)
  851. {
  852. struct ds_data *dpkt = (struct ds_data *) pkt;
  853. struct ds_queue_entry *qp;
  854. qp = kmalloc(sizeof(struct ds_queue_entry) + len, GFP_ATOMIC);
  855. if (!qp) {
  856. __send_ds_nack(dp, dpkt->handle);
  857. } else {
  858. qp->dp = dp;
  859. memcpy(&qp->req, pkt, len);
  860. list_add_tail(&qp->list, &ds_work_list);
  861. wake_up(&ds_wait);
  862. }
  863. return 0;
  864. }
  865. static void ds_up(struct ds_info *dp)
  866. {
  867. struct ldc_channel *lp = dp->lp;
  868. struct ds_ver_req req;
  869. int err;
  870. req.tag.type = DS_INIT_REQ;
  871. req.tag.len = sizeof(req) - sizeof(struct ds_msg_tag);
  872. req.ver.major = 1;
  873. req.ver.minor = 0;
  874. err = __ds_send(lp, &req, sizeof(req));
  875. if (err > 0)
  876. dp->hs_state = DS_HS_START;
  877. }
  878. static void ds_reset(struct ds_info *dp)
  879. {
  880. int i;
  881. dp->hs_state = 0;
  882. for (i = 0; i < dp->num_ds_states; i++) {
  883. struct ds_cap_state *cp = &dp->ds_states[i];
  884. cp->state = CAP_STATE_UNKNOWN;
  885. }
  886. }
  887. static void ds_event(void *arg, int event)
  888. {
  889. struct ds_info *dp = arg;
  890. struct ldc_channel *lp = dp->lp;
  891. unsigned long flags;
  892. int err;
  893. spin_lock_irqsave(&ds_lock, flags);
  894. if (event == LDC_EVENT_UP) {
  895. ds_up(dp);
  896. spin_unlock_irqrestore(&ds_lock, flags);
  897. return;
  898. }
  899. if (event == LDC_EVENT_RESET) {
  900. ds_reset(dp);
  901. spin_unlock_irqrestore(&ds_lock, flags);
  902. return;
  903. }
  904. if (event != LDC_EVENT_DATA_READY) {
  905. printk(KERN_WARNING "ds-%llu: Unexpected LDC event %d\n",
  906. dp->id, event);
  907. spin_unlock_irqrestore(&ds_lock, flags);
  908. return;
  909. }
  910. err = 0;
  911. while (1) {
  912. struct ds_msg_tag *tag;
  913. err = ldc_read(lp, dp->rcv_buf, sizeof(*tag));
  914. if (unlikely(err < 0)) {
  915. if (err == -ECONNRESET)
  916. ds_conn_reset(dp);
  917. break;
  918. }
  919. if (err == 0)
  920. break;
  921. tag = dp->rcv_buf;
  922. err = ldc_read(lp, tag + 1, tag->len);
  923. if (unlikely(err < 0)) {
  924. if (err == -ECONNRESET)
  925. ds_conn_reset(dp);
  926. break;
  927. }
  928. if (err < tag->len)
  929. break;
  930. if (tag->type < DS_DATA)
  931. err = ds_handshake(dp, dp->rcv_buf);
  932. else
  933. err = ds_data(dp, dp->rcv_buf,
  934. sizeof(*tag) + err);
  935. if (err == -ECONNRESET)
  936. break;
  937. }
  938. spin_unlock_irqrestore(&ds_lock, flags);
  939. }
  940. static int __devinit ds_probe(struct vio_dev *vdev,
  941. const struct vio_device_id *id)
  942. {
  943. static int ds_version_printed;
  944. struct ldc_channel_config ds_cfg = {
  945. .event = ds_event,
  946. .mtu = 4096,
  947. .mode = LDC_MODE_STREAM,
  948. };
  949. struct mdesc_handle *hp;
  950. struct ldc_channel *lp;
  951. struct ds_info *dp;
  952. const u64 *val;
  953. int err, i;
  954. if (ds_version_printed++ == 0)
  955. printk(KERN_INFO "%s", version);
  956. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  957. err = -ENOMEM;
  958. if (!dp)
  959. goto out_err;
  960. hp = mdesc_grab();
  961. val = mdesc_get_property(hp, vdev->mp, "id", NULL);
  962. if (val)
  963. dp->id = *val;
  964. mdesc_release(hp);
  965. dp->rcv_buf = kzalloc(4096, GFP_KERNEL);
  966. if (!dp->rcv_buf)
  967. goto out_free_dp;
  968. dp->rcv_buf_len = 4096;
  969. dp->ds_states = kmemdup(ds_states_template,
  970. sizeof(ds_states_template), GFP_KERNEL);
  971. if (!dp->ds_states)
  972. goto out_free_rcv_buf;
  973. dp->num_ds_states = ARRAY_SIZE(ds_states_template);
  974. for (i = 0; i < dp->num_ds_states; i++)
  975. dp->ds_states[i].handle = ((u64)i << 32);
  976. ds_cfg.tx_irq = vdev->tx_irq;
  977. ds_cfg.rx_irq = vdev->rx_irq;
  978. lp = ldc_alloc(vdev->channel_id, &ds_cfg, dp);
  979. if (IS_ERR(lp)) {
  980. err = PTR_ERR(lp);
  981. goto out_free_ds_states;
  982. }
  983. dp->lp = lp;
  984. err = ldc_bind(lp, "DS");
  985. if (err)
  986. goto out_free_ldc;
  987. spin_lock_irq(&ds_lock);
  988. dp->next = ds_info_list;
  989. ds_info_list = dp;
  990. spin_unlock_irq(&ds_lock);
  991. return err;
  992. out_free_ldc:
  993. ldc_free(dp->lp);
  994. out_free_ds_states:
  995. kfree(dp->ds_states);
  996. out_free_rcv_buf:
  997. kfree(dp->rcv_buf);
  998. out_free_dp:
  999. kfree(dp);
  1000. out_err:
  1001. return err;
  1002. }
  1003. static int ds_remove(struct vio_dev *vdev)
  1004. {
  1005. return 0;
  1006. }
  1007. static const struct vio_device_id ds_match[] = {
  1008. {
  1009. .type = "domain-services-port",
  1010. },
  1011. {},
  1012. };
  1013. static struct vio_driver ds_driver = {
  1014. .id_table = ds_match,
  1015. .probe = ds_probe,
  1016. .remove = ds_remove,
  1017. .name = "ds",
  1018. };
  1019. static int __init ds_init(void)
  1020. {
  1021. unsigned long hv_ret, major, minor;
  1022. if (tlb_type == hypervisor) {
  1023. hv_ret = sun4v_get_version(HV_GRP_REBOOT_DATA, &major, &minor);
  1024. if (hv_ret == HV_EOK) {
  1025. pr_info("SUN4V: Reboot data supported (maj=%lu,min=%lu).\n",
  1026. major, minor);
  1027. reboot_data_supported = 1;
  1028. }
  1029. }
  1030. kthread_run(ds_thread, NULL, "kldomd");
  1031. return vio_register_driver(&ds_driver);
  1032. }
  1033. fs_initcall(ds_init);