ACPICA: Namespace: Properly null terminate objects detached from a namespace node
[linux/fpc-iii.git] / arch / sparc / kernel / ds.c
blobdff60abbea012f30a8b55221cc8d7a3a60a762ea
1 /* ds.c: Domain Services driver for Logical Domains
3 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
4 */
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/types.h>
9 #include <linux/string.h>
10 #include <linux/slab.h>
11 #include <linux/sched.h>
12 #include <linux/delay.h>
13 #include <linux/mutex.h>
14 #include <linux/kthread.h>
15 #include <linux/reboot.h>
16 #include <linux/cpu.h>
18 #include <asm/hypervisor.h>
19 #include <asm/ldc.h>
20 #include <asm/vio.h>
21 #include <asm/mdesc.h>
22 #include <asm/head.h>
23 #include <asm/irq.h>
25 #include "kernel.h"
27 #define DRV_MODULE_NAME "ds"
28 #define PFX DRV_MODULE_NAME ": "
29 #define DRV_MODULE_VERSION "1.0"
30 #define DRV_MODULE_RELDATE "Jul 11, 2007"
32 static char version[] =
33 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
34 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
35 MODULE_DESCRIPTION("Sun LDOM domain services driver");
36 MODULE_LICENSE("GPL");
37 MODULE_VERSION(DRV_MODULE_VERSION);
39 struct ds_msg_tag {
40 __u32 type;
41 #define DS_INIT_REQ 0x00
42 #define DS_INIT_ACK 0x01
43 #define DS_INIT_NACK 0x02
44 #define DS_REG_REQ 0x03
45 #define DS_REG_ACK 0x04
46 #define DS_REG_NACK 0x05
47 #define DS_UNREG_REQ 0x06
48 #define DS_UNREG_ACK 0x07
49 #define DS_UNREG_NACK 0x08
50 #define DS_DATA 0x09
51 #define DS_NACK 0x0a
53 __u32 len;
56 /* Result codes */
57 #define DS_OK 0x00
58 #define DS_REG_VER_NACK 0x01
59 #define DS_REG_DUP 0x02
60 #define DS_INV_HDL 0x03
61 #define DS_TYPE_UNKNOWN 0x04
63 struct ds_version {
64 __u16 major;
65 __u16 minor;
68 struct ds_ver_req {
69 struct ds_msg_tag tag;
70 struct ds_version ver;
73 struct ds_ver_ack {
74 struct ds_msg_tag tag;
75 __u16 minor;
78 struct ds_ver_nack {
79 struct ds_msg_tag tag;
80 __u16 major;
83 struct ds_reg_req {
84 struct ds_msg_tag tag;
85 __u64 handle;
86 __u16 major;
87 __u16 minor;
88 char svc_id[0];
91 struct ds_reg_ack {
92 struct ds_msg_tag tag;
93 __u64 handle;
94 __u16 minor;
97 struct ds_reg_nack {
98 struct ds_msg_tag tag;
99 __u64 handle;
100 __u16 major;
103 struct ds_unreg_req {
104 struct ds_msg_tag tag;
105 __u64 handle;
108 struct ds_unreg_ack {
109 struct ds_msg_tag tag;
110 __u64 handle;
113 struct ds_unreg_nack {
114 struct ds_msg_tag tag;
115 __u64 handle;
118 struct ds_data {
119 struct ds_msg_tag tag;
120 __u64 handle;
123 struct ds_data_nack {
124 struct ds_msg_tag tag;
125 __u64 handle;
126 __u64 result;
129 struct ds_info;
130 struct ds_cap_state {
131 __u64 handle;
133 void (*data)(struct ds_info *dp,
134 struct ds_cap_state *cp,
135 void *buf, int len);
137 const char *service_id;
139 u8 state;
140 #define CAP_STATE_UNKNOWN 0x00
141 #define CAP_STATE_REG_SENT 0x01
142 #define CAP_STATE_REGISTERED 0x02
145 static void md_update_data(struct ds_info *dp, struct ds_cap_state *cp,
146 void *buf, int len);
147 static void domain_shutdown_data(struct ds_info *dp,
148 struct ds_cap_state *cp,
149 void *buf, int len);
150 static void domain_panic_data(struct ds_info *dp,
151 struct ds_cap_state *cp,
152 void *buf, int len);
153 #ifdef CONFIG_HOTPLUG_CPU
154 static void dr_cpu_data(struct ds_info *dp,
155 struct ds_cap_state *cp,
156 void *buf, int len);
157 #endif
158 static void ds_pri_data(struct ds_info *dp,
159 struct ds_cap_state *cp,
160 void *buf, int len);
161 static void ds_var_data(struct ds_info *dp,
162 struct ds_cap_state *cp,
163 void *buf, int len);
165 static struct ds_cap_state ds_states_template[] = {
167 .service_id = "md-update",
168 .data = md_update_data,
171 .service_id = "domain-shutdown",
172 .data = domain_shutdown_data,
175 .service_id = "domain-panic",
176 .data = domain_panic_data,
178 #ifdef CONFIG_HOTPLUG_CPU
180 .service_id = "dr-cpu",
181 .data = dr_cpu_data,
183 #endif
185 .service_id = "pri",
186 .data = ds_pri_data,
189 .service_id = "var-config",
190 .data = ds_var_data,
193 .service_id = "var-config-backup",
194 .data = ds_var_data,
198 static DEFINE_SPINLOCK(ds_lock);
200 struct ds_info {
201 struct ldc_channel *lp;
202 u8 hs_state;
203 #define DS_HS_START 0x01
204 #define DS_HS_DONE 0x02
206 u64 id;
208 void *rcv_buf;
209 int rcv_buf_len;
211 struct ds_cap_state *ds_states;
212 int num_ds_states;
214 struct ds_info *next;
217 static struct ds_info *ds_info_list;
219 static struct ds_cap_state *find_cap(struct ds_info *dp, u64 handle)
221 unsigned int index = handle >> 32;
223 if (index >= dp->num_ds_states)
224 return NULL;
225 return &dp->ds_states[index];
228 static struct ds_cap_state *find_cap_by_string(struct ds_info *dp,
229 const char *name)
231 int i;
233 for (i = 0; i < dp->num_ds_states; i++) {
234 if (strcmp(dp->ds_states[i].service_id, name))
235 continue;
237 return &dp->ds_states[i];
239 return NULL;
242 static int __ds_send(struct ldc_channel *lp, void *data, int len)
244 int err, limit = 1000;
246 err = -EINVAL;
247 while (limit-- > 0) {
248 err = ldc_write(lp, data, len);
249 if (!err || (err != -EAGAIN))
250 break;
251 udelay(1);
254 return err;
257 static int ds_send(struct ldc_channel *lp, void *data, int len)
259 unsigned long flags;
260 int err;
262 spin_lock_irqsave(&ds_lock, flags);
263 err = __ds_send(lp, data, len);
264 spin_unlock_irqrestore(&ds_lock, flags);
266 return err;
269 struct ds_md_update_req {
270 __u64 req_num;
273 struct ds_md_update_res {
274 __u64 req_num;
275 __u32 result;
278 static void md_update_data(struct ds_info *dp,
279 struct ds_cap_state *cp,
280 void *buf, int len)
282 struct ldc_channel *lp = dp->lp;
283 struct ds_data *dpkt = buf;
284 struct ds_md_update_req *rp;
285 struct {
286 struct ds_data data;
287 struct ds_md_update_res res;
288 } pkt;
290 rp = (struct ds_md_update_req *) (dpkt + 1);
292 printk(KERN_INFO "ds-%llu: Machine description update.\n", dp->id);
294 mdesc_update();
296 memset(&pkt, 0, sizeof(pkt));
297 pkt.data.tag.type = DS_DATA;
298 pkt.data.tag.len = sizeof(pkt) - sizeof(struct ds_msg_tag);
299 pkt.data.handle = cp->handle;
300 pkt.res.req_num = rp->req_num;
301 pkt.res.result = DS_OK;
303 ds_send(lp, &pkt, sizeof(pkt));
306 struct ds_shutdown_req {
307 __u64 req_num;
308 __u32 ms_delay;
311 struct ds_shutdown_res {
312 __u64 req_num;
313 __u32 result;
314 char reason[1];
317 static void domain_shutdown_data(struct ds_info *dp,
318 struct ds_cap_state *cp,
319 void *buf, int len)
321 struct ldc_channel *lp = dp->lp;
322 struct ds_data *dpkt = buf;
323 struct ds_shutdown_req *rp;
324 struct {
325 struct ds_data data;
326 struct ds_shutdown_res res;
327 } pkt;
329 rp = (struct ds_shutdown_req *) (dpkt + 1);
331 printk(KERN_ALERT "ds-%llu: Shutdown request from "
332 "LDOM manager received.\n", dp->id);
334 memset(&pkt, 0, sizeof(pkt));
335 pkt.data.tag.type = DS_DATA;
336 pkt.data.tag.len = sizeof(pkt) - sizeof(struct ds_msg_tag);
337 pkt.data.handle = cp->handle;
338 pkt.res.req_num = rp->req_num;
339 pkt.res.result = DS_OK;
340 pkt.res.reason[0] = 0;
342 ds_send(lp, &pkt, sizeof(pkt));
344 orderly_poweroff(true);
347 struct ds_panic_req {
348 __u64 req_num;
351 struct ds_panic_res {
352 __u64 req_num;
353 __u32 result;
354 char reason[1];
357 static void domain_panic_data(struct ds_info *dp,
358 struct ds_cap_state *cp,
359 void *buf, int len)
361 struct ldc_channel *lp = dp->lp;
362 struct ds_data *dpkt = buf;
363 struct ds_panic_req *rp;
364 struct {
365 struct ds_data data;
366 struct ds_panic_res res;
367 } pkt;
369 rp = (struct ds_panic_req *) (dpkt + 1);
371 printk(KERN_ALERT "ds-%llu: Panic request from "
372 "LDOM manager received.\n", dp->id);
374 memset(&pkt, 0, sizeof(pkt));
375 pkt.data.tag.type = DS_DATA;
376 pkt.data.tag.len = sizeof(pkt) - sizeof(struct ds_msg_tag);
377 pkt.data.handle = cp->handle;
378 pkt.res.req_num = rp->req_num;
379 pkt.res.result = DS_OK;
380 pkt.res.reason[0] = 0;
382 ds_send(lp, &pkt, sizeof(pkt));
384 panic("PANIC requested by LDOM manager.");
387 #ifdef CONFIG_HOTPLUG_CPU
388 struct dr_cpu_tag {
389 __u64 req_num;
390 __u32 type;
391 #define DR_CPU_CONFIGURE 0x43
392 #define DR_CPU_UNCONFIGURE 0x55
393 #define DR_CPU_FORCE_UNCONFIGURE 0x46
394 #define DR_CPU_STATUS 0x53
396 /* Responses */
397 #define DR_CPU_OK 0x6f
398 #define DR_CPU_ERROR 0x65
400 __u32 num_records;
403 struct dr_cpu_resp_entry {
404 __u32 cpu;
405 __u32 result;
406 #define DR_CPU_RES_OK 0x00
407 #define DR_CPU_RES_FAILURE 0x01
408 #define DR_CPU_RES_BLOCKED 0x02
409 #define DR_CPU_RES_CPU_NOT_RESPONDING 0x03
410 #define DR_CPU_RES_NOT_IN_MD 0x04
412 __u32 stat;
413 #define DR_CPU_STAT_NOT_PRESENT 0x00
414 #define DR_CPU_STAT_UNCONFIGURED 0x01
415 #define DR_CPU_STAT_CONFIGURED 0x02
417 __u32 str_off;
420 static void __dr_cpu_send_error(struct ds_info *dp,
421 struct ds_cap_state *cp,
422 struct ds_data *data)
424 struct dr_cpu_tag *tag = (struct dr_cpu_tag *) (data + 1);
425 struct {
426 struct ds_data data;
427 struct dr_cpu_tag tag;
428 } pkt;
429 int msg_len;
431 memset(&pkt, 0, sizeof(pkt));
432 pkt.data.tag.type = DS_DATA;
433 pkt.data.handle = cp->handle;
434 pkt.tag.req_num = tag->req_num;
435 pkt.tag.type = DR_CPU_ERROR;
436 pkt.tag.num_records = 0;
438 msg_len = (sizeof(struct ds_data) +
439 sizeof(struct dr_cpu_tag));
441 pkt.data.tag.len = msg_len - sizeof(struct ds_msg_tag);
443 __ds_send(dp->lp, &pkt, msg_len);
446 static void dr_cpu_send_error(struct ds_info *dp,
447 struct ds_cap_state *cp,
448 struct ds_data *data)
450 unsigned long flags;
452 spin_lock_irqsave(&ds_lock, flags);
453 __dr_cpu_send_error(dp, cp, data);
454 spin_unlock_irqrestore(&ds_lock, flags);
457 #define CPU_SENTINEL 0xffffffff
459 static void purge_dups(u32 *list, u32 num_ents)
461 unsigned int i;
463 for (i = 0; i < num_ents; i++) {
464 u32 cpu = list[i];
465 unsigned int j;
467 if (cpu == CPU_SENTINEL)
468 continue;
470 for (j = i + 1; j < num_ents; j++) {
471 if (list[j] == cpu)
472 list[j] = CPU_SENTINEL;
477 static int dr_cpu_size_response(int ncpus)
479 return (sizeof(struct ds_data) +
480 sizeof(struct dr_cpu_tag) +
481 (sizeof(struct dr_cpu_resp_entry) * ncpus));
484 static void dr_cpu_init_response(struct ds_data *resp, u64 req_num,
485 u64 handle, int resp_len, int ncpus,
486 cpumask_t *mask, u32 default_stat)
488 struct dr_cpu_resp_entry *ent;
489 struct dr_cpu_tag *tag;
490 int i, cpu;
492 tag = (struct dr_cpu_tag *) (resp + 1);
493 ent = (struct dr_cpu_resp_entry *) (tag + 1);
495 resp->tag.type = DS_DATA;
496 resp->tag.len = resp_len - sizeof(struct ds_msg_tag);
497 resp->handle = handle;
498 tag->req_num = req_num;
499 tag->type = DR_CPU_OK;
500 tag->num_records = ncpus;
502 i = 0;
503 for_each_cpu(cpu, mask) {
504 ent[i].cpu = cpu;
505 ent[i].result = DR_CPU_RES_OK;
506 ent[i].stat = default_stat;
507 i++;
509 BUG_ON(i != ncpus);
512 static void dr_cpu_mark(struct ds_data *resp, int cpu, int ncpus,
513 u32 res, u32 stat)
515 struct dr_cpu_resp_entry *ent;
516 struct dr_cpu_tag *tag;
517 int i;
519 tag = (struct dr_cpu_tag *) (resp + 1);
520 ent = (struct dr_cpu_resp_entry *) (tag + 1);
522 for (i = 0; i < ncpus; i++) {
523 if (ent[i].cpu != cpu)
524 continue;
525 ent[i].result = res;
526 ent[i].stat = stat;
527 break;
531 static int dr_cpu_configure(struct ds_info *dp, struct ds_cap_state *cp,
532 u64 req_num, cpumask_t *mask)
534 struct ds_data *resp;
535 int resp_len, ncpus, cpu;
536 unsigned long flags;
538 ncpus = cpumask_weight(mask);
539 resp_len = dr_cpu_size_response(ncpus);
540 resp = kzalloc(resp_len, GFP_KERNEL);
541 if (!resp)
542 return -ENOMEM;
544 dr_cpu_init_response(resp, req_num, cp->handle,
545 resp_len, ncpus, mask,
546 DR_CPU_STAT_CONFIGURED);
548 mdesc_populate_present_mask(mask);
549 mdesc_fill_in_cpu_data(mask);
551 for_each_cpu(cpu, mask) {
552 int err;
554 printk(KERN_INFO "ds-%llu: Starting cpu %d...\n",
555 dp->id, cpu);
556 err = cpu_up(cpu);
557 if (err) {
558 __u32 res = DR_CPU_RES_FAILURE;
559 __u32 stat = DR_CPU_STAT_UNCONFIGURED;
561 if (!cpu_present(cpu)) {
562 /* CPU not present in MD */
563 res = DR_CPU_RES_NOT_IN_MD;
564 stat = DR_CPU_STAT_NOT_PRESENT;
565 } else if (err == -ENODEV) {
566 /* CPU did not call in successfully */
567 res = DR_CPU_RES_CPU_NOT_RESPONDING;
570 printk(KERN_INFO "ds-%llu: CPU startup failed err=%d\n",
571 dp->id, err);
572 dr_cpu_mark(resp, cpu, ncpus, res, stat);
576 spin_lock_irqsave(&ds_lock, flags);
577 __ds_send(dp->lp, resp, resp_len);
578 spin_unlock_irqrestore(&ds_lock, flags);
580 kfree(resp);
582 /* Redistribute IRQs, taking into account the new cpus. */
583 fixup_irqs();
585 return 0;
588 static int dr_cpu_unconfigure(struct ds_info *dp,
589 struct ds_cap_state *cp,
590 u64 req_num,
591 cpumask_t *mask)
593 struct ds_data *resp;
594 int resp_len, ncpus, cpu;
595 unsigned long flags;
597 ncpus = cpumask_weight(mask);
598 resp_len = dr_cpu_size_response(ncpus);
599 resp = kzalloc(resp_len, GFP_KERNEL);
600 if (!resp)
601 return -ENOMEM;
603 dr_cpu_init_response(resp, req_num, cp->handle,
604 resp_len, ncpus, mask,
605 DR_CPU_STAT_UNCONFIGURED);
607 for_each_cpu(cpu, mask) {
608 int err;
610 printk(KERN_INFO "ds-%llu: Shutting down cpu %d...\n",
611 dp->id, cpu);
612 err = cpu_down(cpu);
613 if (err)
614 dr_cpu_mark(resp, cpu, ncpus,
615 DR_CPU_RES_FAILURE,
616 DR_CPU_STAT_CONFIGURED);
619 spin_lock_irqsave(&ds_lock, flags);
620 __ds_send(dp->lp, resp, resp_len);
621 spin_unlock_irqrestore(&ds_lock, flags);
623 kfree(resp);
625 return 0;
628 static void dr_cpu_data(struct ds_info *dp, struct ds_cap_state *cp, void *buf,
629 int len)
631 struct ds_data *data = buf;
632 struct dr_cpu_tag *tag = (struct dr_cpu_tag *) (data + 1);
633 u32 *cpu_list = (u32 *) (tag + 1);
634 u64 req_num = tag->req_num;
635 cpumask_t mask;
636 unsigned int i;
637 int err;
639 switch (tag->type) {
640 case DR_CPU_CONFIGURE:
641 case DR_CPU_UNCONFIGURE:
642 case DR_CPU_FORCE_UNCONFIGURE:
643 break;
645 default:
646 dr_cpu_send_error(dp, cp, data);
647 return;
650 purge_dups(cpu_list, tag->num_records);
652 cpumask_clear(&mask);
653 for (i = 0; i < tag->num_records; i++) {
654 if (cpu_list[i] == CPU_SENTINEL)
655 continue;
657 if (cpu_list[i] < nr_cpu_ids)
658 cpumask_set_cpu(cpu_list[i], &mask);
661 if (tag->type == DR_CPU_CONFIGURE)
662 err = dr_cpu_configure(dp, cp, req_num, &mask);
663 else
664 err = dr_cpu_unconfigure(dp, cp, req_num, &mask);
666 if (err)
667 dr_cpu_send_error(dp, cp, data);
669 #endif /* CONFIG_HOTPLUG_CPU */
671 struct ds_pri_msg {
672 __u64 req_num;
673 __u64 type;
674 #define DS_PRI_REQUEST 0x00
675 #define DS_PRI_DATA 0x01
676 #define DS_PRI_UPDATE 0x02
679 static void ds_pri_data(struct ds_info *dp,
680 struct ds_cap_state *cp,
681 void *buf, int len)
683 struct ds_data *dpkt = buf;
684 struct ds_pri_msg *rp;
686 rp = (struct ds_pri_msg *) (dpkt + 1);
688 printk(KERN_INFO "ds-%llu: PRI REQ [%llx:%llx], len=%d\n",
689 dp->id, rp->req_num, rp->type, len);
692 struct ds_var_hdr {
693 __u32 type;
694 #define DS_VAR_SET_REQ 0x00
695 #define DS_VAR_DELETE_REQ 0x01
696 #define DS_VAR_SET_RESP 0x02
697 #define DS_VAR_DELETE_RESP 0x03
700 struct ds_var_set_msg {
701 struct ds_var_hdr hdr;
702 char name_and_value[0];
705 struct ds_var_delete_msg {
706 struct ds_var_hdr hdr;
707 char name[0];
710 struct ds_var_resp {
711 struct ds_var_hdr hdr;
712 __u32 result;
713 #define DS_VAR_SUCCESS 0x00
714 #define DS_VAR_NO_SPACE 0x01
715 #define DS_VAR_INVALID_VAR 0x02
716 #define DS_VAR_INVALID_VAL 0x03
717 #define DS_VAR_NOT_PRESENT 0x04
720 static DEFINE_MUTEX(ds_var_mutex);
721 static int ds_var_doorbell;
722 static int ds_var_response;
724 static void ds_var_data(struct ds_info *dp,
725 struct ds_cap_state *cp,
726 void *buf, int len)
728 struct ds_data *dpkt = buf;
729 struct ds_var_resp *rp;
731 rp = (struct ds_var_resp *) (dpkt + 1);
733 if (rp->hdr.type != DS_VAR_SET_RESP &&
734 rp->hdr.type != DS_VAR_DELETE_RESP)
735 return;
737 ds_var_response = rp->result;
738 wmb();
739 ds_var_doorbell = 1;
742 void ldom_set_var(const char *var, const char *value)
744 struct ds_cap_state *cp;
745 struct ds_info *dp;
746 unsigned long flags;
748 spin_lock_irqsave(&ds_lock, flags);
749 cp = NULL;
750 for (dp = ds_info_list; dp; dp = dp->next) {
751 struct ds_cap_state *tmp;
753 tmp = find_cap_by_string(dp, "var-config");
754 if (tmp && tmp->state == CAP_STATE_REGISTERED) {
755 cp = tmp;
756 break;
759 if (!cp) {
760 for (dp = ds_info_list; dp; dp = dp->next) {
761 struct ds_cap_state *tmp;
763 tmp = find_cap_by_string(dp, "var-config-backup");
764 if (tmp && tmp->state == CAP_STATE_REGISTERED) {
765 cp = tmp;
766 break;
770 spin_unlock_irqrestore(&ds_lock, flags);
772 if (cp) {
773 union {
774 struct {
775 struct ds_data data;
776 struct ds_var_set_msg msg;
777 } header;
778 char all[512];
779 } pkt;
780 char *base, *p;
781 int msg_len, loops;
783 if (strlen(var) + strlen(value) + 2 >
784 sizeof(pkt) - sizeof(pkt.header)) {
785 printk(KERN_ERR PFX
786 "contents length: %zu, which more than max: %lu,"
787 "so could not set (%s) variable to (%s).\n",
788 strlen(var) + strlen(value) + 2,
789 sizeof(pkt) - sizeof(pkt.header), var, value);
790 return;
793 memset(&pkt, 0, sizeof(pkt));
794 pkt.header.data.tag.type = DS_DATA;
795 pkt.header.data.handle = cp->handle;
796 pkt.header.msg.hdr.type = DS_VAR_SET_REQ;
797 base = p = &pkt.header.msg.name_and_value[0];
798 strcpy(p, var);
799 p += strlen(var) + 1;
800 strcpy(p, value);
801 p += strlen(value) + 1;
803 msg_len = (sizeof(struct ds_data) +
804 sizeof(struct ds_var_set_msg) +
805 (p - base));
806 msg_len = (msg_len + 3) & ~3;
807 pkt.header.data.tag.len = msg_len - sizeof(struct ds_msg_tag);
809 mutex_lock(&ds_var_mutex);
811 spin_lock_irqsave(&ds_lock, flags);
812 ds_var_doorbell = 0;
813 ds_var_response = -1;
815 __ds_send(dp->lp, &pkt, msg_len);
816 spin_unlock_irqrestore(&ds_lock, flags);
818 loops = 1000;
819 while (ds_var_doorbell == 0) {
820 if (loops-- < 0)
821 break;
822 barrier();
823 udelay(100);
826 mutex_unlock(&ds_var_mutex);
828 if (ds_var_doorbell == 0 ||
829 ds_var_response != DS_VAR_SUCCESS)
830 printk(KERN_ERR "ds-%llu: var-config [%s:%s] "
831 "failed, response(%d).\n",
832 dp->id, var, value,
833 ds_var_response);
834 } else {
835 printk(KERN_ERR PFX "var-config not registered so "
836 "could not set (%s) variable to (%s).\n",
837 var, value);
841 static char full_boot_str[256] __attribute__((aligned(32)));
842 static int reboot_data_supported;
844 void ldom_reboot(const char *boot_command)
846 /* Don't bother with any of this if the boot_command
847 * is empty.
849 if (boot_command && strlen(boot_command)) {
850 unsigned long len;
852 snprintf(full_boot_str, sizeof(full_boot_str), "boot %s",
853 boot_command);
854 len = strlen(full_boot_str);
856 if (reboot_data_supported) {
857 unsigned long ra = kimage_addr_to_ra(full_boot_str);
858 unsigned long hv_ret;
860 hv_ret = sun4v_reboot_data_set(ra, len);
861 if (hv_ret != HV_EOK)
862 pr_err("SUN4V: Unable to set reboot data "
863 "hv_ret=%lu\n", hv_ret);
864 } else {
865 ldom_set_var("reboot-command", full_boot_str);
868 sun4v_mach_sir();
871 void ldom_power_off(void)
873 sun4v_mach_exit(0);
876 static void ds_conn_reset(struct ds_info *dp)
878 printk(KERN_ERR "ds-%llu: ds_conn_reset() from %pf\n",
879 dp->id, __builtin_return_address(0));
882 static int register_services(struct ds_info *dp)
884 struct ldc_channel *lp = dp->lp;
885 int i;
887 for (i = 0; i < dp->num_ds_states; i++) {
888 struct {
889 struct ds_reg_req req;
890 u8 id_buf[256];
891 } pbuf;
892 struct ds_cap_state *cp = &dp->ds_states[i];
893 int err, msg_len;
894 u64 new_count;
896 if (cp->state == CAP_STATE_REGISTERED)
897 continue;
899 new_count = sched_clock() & 0xffffffff;
900 cp->handle = ((u64) i << 32) | new_count;
902 msg_len = (sizeof(struct ds_reg_req) +
903 strlen(cp->service_id));
905 memset(&pbuf, 0, sizeof(pbuf));
906 pbuf.req.tag.type = DS_REG_REQ;
907 pbuf.req.tag.len = (msg_len - sizeof(struct ds_msg_tag));
908 pbuf.req.handle = cp->handle;
909 pbuf.req.major = 1;
910 pbuf.req.minor = 0;
911 strcpy(pbuf.req.svc_id, cp->service_id);
913 err = __ds_send(lp, &pbuf, msg_len);
914 if (err > 0)
915 cp->state = CAP_STATE_REG_SENT;
917 return 0;
920 static int ds_handshake(struct ds_info *dp, struct ds_msg_tag *pkt)
923 if (dp->hs_state == DS_HS_START) {
924 if (pkt->type != DS_INIT_ACK)
925 goto conn_reset;
927 dp->hs_state = DS_HS_DONE;
929 return register_services(dp);
932 if (dp->hs_state != DS_HS_DONE)
933 goto conn_reset;
935 if (pkt->type == DS_REG_ACK) {
936 struct ds_reg_ack *ap = (struct ds_reg_ack *) pkt;
937 struct ds_cap_state *cp = find_cap(dp, ap->handle);
939 if (!cp) {
940 printk(KERN_ERR "ds-%llu: REG ACK for unknown "
941 "handle %llx\n", dp->id, ap->handle);
942 return 0;
944 printk(KERN_INFO "ds-%llu: Registered %s service.\n",
945 dp->id, cp->service_id);
946 cp->state = CAP_STATE_REGISTERED;
947 } else if (pkt->type == DS_REG_NACK) {
948 struct ds_reg_nack *np = (struct ds_reg_nack *) pkt;
949 struct ds_cap_state *cp = find_cap(dp, np->handle);
951 if (!cp) {
952 printk(KERN_ERR "ds-%llu: REG NACK for "
953 "unknown handle %llx\n",
954 dp->id, np->handle);
955 return 0;
957 cp->state = CAP_STATE_UNKNOWN;
960 return 0;
962 conn_reset:
963 ds_conn_reset(dp);
964 return -ECONNRESET;
967 static void __send_ds_nack(struct ds_info *dp, u64 handle)
969 struct ds_data_nack nack = {
970 .tag = {
971 .type = DS_NACK,
972 .len = (sizeof(struct ds_data_nack) -
973 sizeof(struct ds_msg_tag)),
975 .handle = handle,
976 .result = DS_INV_HDL,
979 __ds_send(dp->lp, &nack, sizeof(nack));
982 static LIST_HEAD(ds_work_list);
983 static DECLARE_WAIT_QUEUE_HEAD(ds_wait);
985 struct ds_queue_entry {
986 struct list_head list;
987 struct ds_info *dp;
988 int req_len;
989 int __pad;
990 u64 req[0];
993 static void process_ds_work(void)
995 struct ds_queue_entry *qp, *tmp;
996 unsigned long flags;
997 LIST_HEAD(todo);
999 spin_lock_irqsave(&ds_lock, flags);
1000 list_splice_init(&ds_work_list, &todo);
1001 spin_unlock_irqrestore(&ds_lock, flags);
1003 list_for_each_entry_safe(qp, tmp, &todo, list) {
1004 struct ds_data *dpkt = (struct ds_data *) qp->req;
1005 struct ds_info *dp = qp->dp;
1006 struct ds_cap_state *cp = find_cap(dp, dpkt->handle);
1007 int req_len = qp->req_len;
1009 if (!cp) {
1010 printk(KERN_ERR "ds-%llu: Data for unknown "
1011 "handle %llu\n",
1012 dp->id, dpkt->handle);
1014 spin_lock_irqsave(&ds_lock, flags);
1015 __send_ds_nack(dp, dpkt->handle);
1016 spin_unlock_irqrestore(&ds_lock, flags);
1017 } else {
1018 cp->data(dp, cp, dpkt, req_len);
1021 list_del(&qp->list);
1022 kfree(qp);
1026 static int ds_thread(void *__unused)
1028 DEFINE_WAIT(wait);
1030 while (1) {
1031 prepare_to_wait(&ds_wait, &wait, TASK_INTERRUPTIBLE);
1032 if (list_empty(&ds_work_list))
1033 schedule();
1034 finish_wait(&ds_wait, &wait);
1036 if (kthread_should_stop())
1037 break;
1039 process_ds_work();
1042 return 0;
1045 static int ds_data(struct ds_info *dp, struct ds_msg_tag *pkt, int len)
1047 struct ds_data *dpkt = (struct ds_data *) pkt;
1048 struct ds_queue_entry *qp;
1050 qp = kmalloc(sizeof(struct ds_queue_entry) + len, GFP_ATOMIC);
1051 if (!qp) {
1052 __send_ds_nack(dp, dpkt->handle);
1053 } else {
1054 qp->dp = dp;
1055 memcpy(&qp->req, pkt, len);
1056 list_add_tail(&qp->list, &ds_work_list);
1057 wake_up(&ds_wait);
1059 return 0;
1062 static void ds_up(struct ds_info *dp)
1064 struct ldc_channel *lp = dp->lp;
1065 struct ds_ver_req req;
1066 int err;
1068 req.tag.type = DS_INIT_REQ;
1069 req.tag.len = sizeof(req) - sizeof(struct ds_msg_tag);
1070 req.ver.major = 1;
1071 req.ver.minor = 0;
1073 err = __ds_send(lp, &req, sizeof(req));
1074 if (err > 0)
1075 dp->hs_state = DS_HS_START;
1078 static void ds_reset(struct ds_info *dp)
1080 int i;
1082 dp->hs_state = 0;
1084 for (i = 0; i < dp->num_ds_states; i++) {
1085 struct ds_cap_state *cp = &dp->ds_states[i];
1087 cp->state = CAP_STATE_UNKNOWN;
1091 static void ds_event(void *arg, int event)
1093 struct ds_info *dp = arg;
1094 struct ldc_channel *lp = dp->lp;
1095 unsigned long flags;
1096 int err;
1098 spin_lock_irqsave(&ds_lock, flags);
1100 if (event == LDC_EVENT_UP) {
1101 ds_up(dp);
1102 spin_unlock_irqrestore(&ds_lock, flags);
1103 return;
1106 if (event == LDC_EVENT_RESET) {
1107 ds_reset(dp);
1108 spin_unlock_irqrestore(&ds_lock, flags);
1109 return;
1112 if (event != LDC_EVENT_DATA_READY) {
1113 printk(KERN_WARNING "ds-%llu: Unexpected LDC event %d\n",
1114 dp->id, event);
1115 spin_unlock_irqrestore(&ds_lock, flags);
1116 return;
1119 err = 0;
1120 while (1) {
1121 struct ds_msg_tag *tag;
1123 err = ldc_read(lp, dp->rcv_buf, sizeof(*tag));
1125 if (unlikely(err < 0)) {
1126 if (err == -ECONNRESET)
1127 ds_conn_reset(dp);
1128 break;
1130 if (err == 0)
1131 break;
1133 tag = dp->rcv_buf;
1134 err = ldc_read(lp, tag + 1, tag->len);
1136 if (unlikely(err < 0)) {
1137 if (err == -ECONNRESET)
1138 ds_conn_reset(dp);
1139 break;
1141 if (err < tag->len)
1142 break;
1144 if (tag->type < DS_DATA)
1145 err = ds_handshake(dp, dp->rcv_buf);
1146 else
1147 err = ds_data(dp, dp->rcv_buf,
1148 sizeof(*tag) + err);
1149 if (err == -ECONNRESET)
1150 break;
1153 spin_unlock_irqrestore(&ds_lock, flags);
1156 static int ds_probe(struct vio_dev *vdev, const struct vio_device_id *id)
1158 static int ds_version_printed;
1159 struct ldc_channel_config ds_cfg = {
1160 .event = ds_event,
1161 .mtu = 4096,
1162 .mode = LDC_MODE_STREAM,
1164 struct mdesc_handle *hp;
1165 struct ldc_channel *lp;
1166 struct ds_info *dp;
1167 const u64 *val;
1168 int err, i;
1170 if (ds_version_printed++ == 0)
1171 printk(KERN_INFO "%s", version);
1173 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1174 err = -ENOMEM;
1175 if (!dp)
1176 goto out_err;
1178 hp = mdesc_grab();
1179 val = mdesc_get_property(hp, vdev->mp, "id", NULL);
1180 if (val)
1181 dp->id = *val;
1182 mdesc_release(hp);
1184 dp->rcv_buf = kzalloc(4096, GFP_KERNEL);
1185 if (!dp->rcv_buf)
1186 goto out_free_dp;
1188 dp->rcv_buf_len = 4096;
1190 dp->ds_states = kmemdup(ds_states_template,
1191 sizeof(ds_states_template), GFP_KERNEL);
1192 if (!dp->ds_states)
1193 goto out_free_rcv_buf;
1195 dp->num_ds_states = ARRAY_SIZE(ds_states_template);
1197 for (i = 0; i < dp->num_ds_states; i++)
1198 dp->ds_states[i].handle = ((u64)i << 32);
1200 ds_cfg.tx_irq = vdev->tx_irq;
1201 ds_cfg.rx_irq = vdev->rx_irq;
1203 lp = ldc_alloc(vdev->channel_id, &ds_cfg, dp);
1204 if (IS_ERR(lp)) {
1205 err = PTR_ERR(lp);
1206 goto out_free_ds_states;
1208 dp->lp = lp;
1210 err = ldc_bind(lp, "DS");
1211 if (err)
1212 goto out_free_ldc;
1214 spin_lock_irq(&ds_lock);
1215 dp->next = ds_info_list;
1216 ds_info_list = dp;
1217 spin_unlock_irq(&ds_lock);
1219 return err;
1221 out_free_ldc:
1222 ldc_free(dp->lp);
1224 out_free_ds_states:
1225 kfree(dp->ds_states);
1227 out_free_rcv_buf:
1228 kfree(dp->rcv_buf);
1230 out_free_dp:
1231 kfree(dp);
1233 out_err:
1234 return err;
1237 static int ds_remove(struct vio_dev *vdev)
1239 return 0;
1242 static const struct vio_device_id ds_match[] = {
1244 .type = "domain-services-port",
1249 static struct vio_driver ds_driver = {
1250 .id_table = ds_match,
1251 .probe = ds_probe,
1252 .remove = ds_remove,
1253 .name = "ds",
1256 static int __init ds_init(void)
1258 unsigned long hv_ret, major, minor;
1260 if (tlb_type == hypervisor) {
1261 hv_ret = sun4v_get_version(HV_GRP_REBOOT_DATA, &major, &minor);
1262 if (hv_ret == HV_EOK) {
1263 pr_info("SUN4V: Reboot data supported (maj=%lu,min=%lu).\n",
1264 major, minor);
1265 reboot_data_supported = 1;
1268 kthread_run(ds_thread, NULL, "kldomd");
1270 return vio_register_driver(&ds_driver);
1273 fs_initcall(ds_init);