[SPARC64]: Give more accurate errors in dr_cpu_configure().
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / sparc64 / kernel / ds.c
blobb6331718ee0c6c1dd7e420d63c5735eadde00a04
1 /* ds.c: Domain Services driver for Logical Domains
3 * Copyright (C) 2007 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/module.h>
10 #include <linux/string.h>
11 #include <linux/slab.h>
12 #include <linux/sched.h>
13 #include <linux/delay.h>
14 #include <linux/mutex.h>
15 #include <linux/kthread.h>
16 #include <linux/cpu.h>
18 #include <asm/ldc.h>
19 #include <asm/vio.h>
20 #include <asm/power.h>
21 #include <asm/mdesc.h>
22 #include <asm/head.h>
24 #define DRV_MODULE_NAME "ds"
25 #define PFX DRV_MODULE_NAME ": "
26 #define DRV_MODULE_VERSION "1.0"
27 #define DRV_MODULE_RELDATE "Jul 11, 2007"
29 static char version[] __devinitdata =
30 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
31 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
32 MODULE_DESCRIPTION("Sun LDOM domain services driver");
33 MODULE_LICENSE("GPL");
34 MODULE_VERSION(DRV_MODULE_VERSION);
36 struct ds_msg_tag {
37 __u32 type;
38 #define DS_INIT_REQ 0x00
39 #define DS_INIT_ACK 0x01
40 #define DS_INIT_NACK 0x02
41 #define DS_REG_REQ 0x03
42 #define DS_REG_ACK 0x04
43 #define DS_REG_NACK 0x05
44 #define DS_UNREG_REQ 0x06
45 #define DS_UNREG_ACK 0x07
46 #define DS_UNREG_NACK 0x08
47 #define DS_DATA 0x09
48 #define DS_NACK 0x0a
50 __u32 len;
53 /* Result codes */
54 #define DS_OK 0x00
55 #define DS_REG_VER_NACK 0x01
56 #define DS_REG_DUP 0x02
57 #define DS_INV_HDL 0x03
58 #define DS_TYPE_UNKNOWN 0x04
60 struct ds_version {
61 __u16 major;
62 __u16 minor;
65 struct ds_ver_req {
66 struct ds_msg_tag tag;
67 struct ds_version ver;
70 struct ds_ver_ack {
71 struct ds_msg_tag tag;
72 __u16 minor;
75 struct ds_ver_nack {
76 struct ds_msg_tag tag;
77 __u16 major;
80 struct ds_reg_req {
81 struct ds_msg_tag tag;
82 __u64 handle;
83 __u16 major;
84 __u16 minor;
85 char svc_id[0];
88 struct ds_reg_ack {
89 struct ds_msg_tag tag;
90 __u64 handle;
91 __u16 minor;
94 struct ds_reg_nack {
95 struct ds_msg_tag tag;
96 __u64 handle;
97 __u16 major;
100 struct ds_unreg_req {
101 struct ds_msg_tag tag;
102 __u64 handle;
105 struct ds_unreg_ack {
106 struct ds_msg_tag tag;
107 __u64 handle;
110 struct ds_unreg_nack {
111 struct ds_msg_tag tag;
112 __u64 handle;
115 struct ds_data {
116 struct ds_msg_tag tag;
117 __u64 handle;
120 struct ds_data_nack {
121 struct ds_msg_tag tag;
122 __u64 handle;
123 __u64 result;
126 struct ds_cap_state {
127 __u64 handle;
129 void (*data)(struct ldc_channel *lp,
130 struct ds_cap_state *cp,
131 void *buf, int len);
133 const char *service_id;
135 u8 state;
136 #define CAP_STATE_UNKNOWN 0x00
137 #define CAP_STATE_REG_SENT 0x01
138 #define CAP_STATE_REGISTERED 0x02
141 static void md_update_data(struct ldc_channel *lp, struct ds_cap_state *cp,
142 void *buf, int len);
143 static void domain_shutdown_data(struct ldc_channel *lp,
144 struct ds_cap_state *cp,
145 void *buf, int len);
146 static void domain_panic_data(struct ldc_channel *lp,
147 struct ds_cap_state *cp,
148 void *buf, int len);
149 #ifdef CONFIG_HOTPLUG_CPU
150 static void dr_cpu_data(struct ldc_channel *lp,
151 struct ds_cap_state *cp,
152 void *buf, int len);
153 #endif
154 static void ds_pri_data(struct ldc_channel *lp,
155 struct ds_cap_state *cp,
156 void *buf, int len);
157 static void ds_var_data(struct ldc_channel *lp,
158 struct ds_cap_state *cp,
159 void *buf, int len);
161 struct ds_cap_state ds_states[] = {
163 .service_id = "md-update",
164 .data = md_update_data,
167 .service_id = "domain-shutdown",
168 .data = domain_shutdown_data,
171 .service_id = "domain-panic",
172 .data = domain_panic_data,
174 #ifdef CONFIG_HOTPLUG_CPU
176 .service_id = "dr-cpu",
177 .data = dr_cpu_data,
179 #endif
181 .service_id = "pri",
182 .data = ds_pri_data,
185 .service_id = "var-config",
186 .data = ds_var_data,
189 .service_id = "var-config-backup",
190 .data = ds_var_data,
194 static DEFINE_SPINLOCK(ds_lock);
196 struct ds_info {
197 struct ldc_channel *lp;
198 u8 hs_state;
199 #define DS_HS_START 0x01
200 #define DS_HS_DONE 0x02
202 void *rcv_buf;
203 int rcv_buf_len;
206 static struct ds_info *ds_info;
208 static struct ds_cap_state *find_cap(u64 handle)
210 unsigned int index = handle >> 32;
212 if (index >= ARRAY_SIZE(ds_states))
213 return NULL;
214 return &ds_states[index];
217 static struct ds_cap_state *find_cap_by_string(const char *name)
219 int i;
221 for (i = 0; i < ARRAY_SIZE(ds_states); i++) {
222 if (strcmp(ds_states[i].service_id, name))
223 continue;
225 return &ds_states[i];
227 return NULL;
230 static int ds_send(struct ldc_channel *lp, void *data, int len)
232 int err, limit = 1000;
234 err = -EINVAL;
235 while (limit-- > 0) {
236 err = ldc_write(lp, data, len);
237 if (!err || (err != -EAGAIN))
238 break;
239 udelay(1);
242 return err;
245 struct ds_md_update_req {
246 __u64 req_num;
249 struct ds_md_update_res {
250 __u64 req_num;
251 __u32 result;
254 static void md_update_data(struct ldc_channel *lp,
255 struct ds_cap_state *dp,
256 void *buf, int len)
258 struct ds_data *dpkt = buf;
259 struct ds_md_update_req *rp;
260 struct {
261 struct ds_data data;
262 struct ds_md_update_res res;
263 } pkt;
265 rp = (struct ds_md_update_req *) (dpkt + 1);
267 printk(KERN_INFO PFX "Machine description update.\n");
269 memset(&pkt, 0, sizeof(pkt));
270 pkt.data.tag.type = DS_DATA;
271 pkt.data.tag.len = sizeof(pkt) - sizeof(struct ds_msg_tag);
272 pkt.data.handle = dp->handle;
273 pkt.res.req_num = rp->req_num;
274 pkt.res.result = DS_OK;
276 ds_send(lp, &pkt, sizeof(pkt));
278 mdesc_update();
281 struct ds_shutdown_req {
282 __u64 req_num;
283 __u32 ms_delay;
286 struct ds_shutdown_res {
287 __u64 req_num;
288 __u32 result;
289 char reason[1];
292 static void domain_shutdown_data(struct ldc_channel *lp,
293 struct ds_cap_state *dp,
294 void *buf, int len)
296 struct ds_data *dpkt = buf;
297 struct ds_shutdown_req *rp;
298 struct {
299 struct ds_data data;
300 struct ds_shutdown_res res;
301 } pkt;
303 rp = (struct ds_shutdown_req *) (dpkt + 1);
305 printk(KERN_ALERT PFX "Shutdown request from "
306 "LDOM manager received.\n");
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 = dp->handle;
312 pkt.res.req_num = rp->req_num;
313 pkt.res.result = DS_OK;
314 pkt.res.reason[0] = 0;
316 ds_send(lp, &pkt, sizeof(pkt));
318 wake_up_powerd();
321 struct ds_panic_req {
322 __u64 req_num;
325 struct ds_panic_res {
326 __u64 req_num;
327 __u32 result;
328 char reason[1];
331 static void domain_panic_data(struct ldc_channel *lp,
332 struct ds_cap_state *dp,
333 void *buf, int len)
335 struct ds_data *dpkt = buf;
336 struct ds_panic_req *rp;
337 struct {
338 struct ds_data data;
339 struct ds_panic_res res;
340 } pkt;
342 rp = (struct ds_panic_req *) (dpkt + 1);
344 printk(KERN_ALERT PFX "Panic request from "
345 "LDOM manager received.\n");
347 memset(&pkt, 0, sizeof(pkt));
348 pkt.data.tag.type = DS_DATA;
349 pkt.data.tag.len = sizeof(pkt) - sizeof(struct ds_msg_tag);
350 pkt.data.handle = dp->handle;
351 pkt.res.req_num = rp->req_num;
352 pkt.res.result = DS_OK;
353 pkt.res.reason[0] = 0;
355 ds_send(lp, &pkt, sizeof(pkt));
357 panic("PANIC requested by LDOM manager.");
360 #ifdef CONFIG_HOTPLUG_CPU
361 struct dr_cpu_tag {
362 __u64 req_num;
363 __u32 type;
364 #define DR_CPU_CONFIGURE 0x43
365 #define DR_CPU_UNCONFIGURE 0x55
366 #define DR_CPU_FORCE_UNCONFIGURE 0x46
367 #define DR_CPU_STATUS 0x53
369 /* Responses */
370 #define DR_CPU_OK 0x6f
371 #define DR_CPU_ERROR 0x65
373 __u32 num_records;
376 struct dr_cpu_resp_entry {
377 __u32 cpu;
378 __u32 result;
379 #define DR_CPU_RES_OK 0x00
380 #define DR_CPU_RES_FAILURE 0x01
381 #define DR_CPU_RES_BLOCKED 0x02
382 #define DR_CPU_RES_CPU_NOT_RESPONDING 0x03
383 #define DR_CPU_RES_NOT_IN_MD 0x04
385 __u32 stat;
386 #define DR_CPU_STAT_NOT_PRESENT 0x00
387 #define DR_CPU_STAT_UNCONFIGURED 0x01
388 #define DR_CPU_STAT_CONFIGURED 0x02
390 __u32 str_off;
393 /* DR cpu requests get queued onto the work list by the
394 * dr_cpu_data() callback. The list is protected by
395 * ds_lock, and processed by dr_cpu_process() in order.
397 static LIST_HEAD(dr_cpu_work_list);
398 static DECLARE_WAIT_QUEUE_HEAD(dr_cpu_wait);
400 struct dr_cpu_queue_entry {
401 struct list_head list;
402 char req[0];
405 static void __dr_cpu_send_error(struct ds_cap_state *cp, struct ds_data *data)
407 struct dr_cpu_tag *tag = (struct dr_cpu_tag *) (data + 1);
408 struct ds_info *dp = ds_info;
409 struct {
410 struct ds_data data;
411 struct dr_cpu_tag tag;
412 } pkt;
413 int msg_len;
415 memset(&pkt, 0, sizeof(pkt));
416 pkt.data.tag.type = DS_DATA;
417 pkt.data.handle = cp->handle;
418 pkt.tag.req_num = tag->req_num;
419 pkt.tag.type = DR_CPU_ERROR;
420 pkt.tag.num_records = 0;
422 msg_len = (sizeof(struct ds_data) +
423 sizeof(struct dr_cpu_tag));
425 pkt.data.tag.len = msg_len - sizeof(struct ds_msg_tag);
427 ds_send(dp->lp, &pkt, msg_len);
430 static void dr_cpu_send_error(struct ds_cap_state *cp, struct ds_data *data)
432 unsigned long flags;
434 spin_lock_irqsave(&ds_lock, flags);
435 __dr_cpu_send_error(cp, data);
436 spin_unlock_irqrestore(&ds_lock, flags);
439 #define CPU_SENTINEL 0xffffffff
441 static void purge_dups(u32 *list, u32 num_ents)
443 unsigned int i;
445 for (i = 0; i < num_ents; i++) {
446 u32 cpu = list[i];
447 unsigned int j;
449 if (cpu == CPU_SENTINEL)
450 continue;
452 for (j = i + 1; j < num_ents; j++) {
453 if (list[j] == cpu)
454 list[j] = CPU_SENTINEL;
459 static int dr_cpu_size_response(int ncpus)
461 return (sizeof(struct ds_data) +
462 sizeof(struct dr_cpu_tag) +
463 (sizeof(struct dr_cpu_resp_entry) * ncpus));
466 static void dr_cpu_init_response(struct ds_data *resp, u64 req_num,
467 u64 handle, int resp_len, int ncpus,
468 cpumask_t *mask, u32 default_stat)
470 struct dr_cpu_resp_entry *ent;
471 struct dr_cpu_tag *tag;
472 int i, cpu;
474 tag = (struct dr_cpu_tag *) (resp + 1);
475 ent = (struct dr_cpu_resp_entry *) (tag + 1);
477 resp->tag.type = DS_DATA;
478 resp->tag.len = resp_len - sizeof(struct ds_msg_tag);
479 resp->handle = handle;
480 tag->req_num = req_num;
481 tag->type = DR_CPU_OK;
482 tag->num_records = ncpus;
484 i = 0;
485 for_each_cpu_mask(cpu, *mask) {
486 ent[i].cpu = cpu;
487 ent[i].result = DR_CPU_RES_OK;
488 ent[i].stat = default_stat;
489 i++;
491 BUG_ON(i != ncpus);
494 static void dr_cpu_mark(struct ds_data *resp, int cpu, int ncpus,
495 u32 res, u32 stat)
497 struct dr_cpu_resp_entry *ent;
498 struct dr_cpu_tag *tag;
499 int i;
501 tag = (struct dr_cpu_tag *) (resp + 1);
502 ent = (struct dr_cpu_resp_entry *) (tag + 1);
504 for (i = 0; i < ncpus; i++) {
505 if (ent[i].cpu != cpu)
506 continue;
507 ent[i].result = res;
508 ent[i].stat = stat;
509 break;
513 static int dr_cpu_configure(struct ds_cap_state *cp, u64 req_num,
514 cpumask_t *mask)
516 struct ds_data *resp;
517 int resp_len, ncpus, cpu;
518 unsigned long flags;
520 ncpus = cpus_weight(*mask);
521 resp_len = dr_cpu_size_response(ncpus);
522 resp = kzalloc(resp_len, GFP_KERNEL);
523 if (!resp)
524 return -ENOMEM;
526 dr_cpu_init_response(resp, req_num, cp->handle,
527 resp_len, ncpus, mask,
528 DR_CPU_STAT_CONFIGURED);
530 mdesc_fill_in_cpu_data(*mask);
532 for_each_cpu_mask(cpu, *mask) {
533 int err;
535 printk(KERN_INFO PFX "Starting cpu %d...\n", cpu);
536 err = cpu_up(cpu);
537 if (err) {
538 __u32 res = DR_CPU_RES_FAILURE;
539 __u32 stat = DR_CPU_STAT_UNCONFIGURED;
541 if (!cpu_present(cpu)) {
542 /* CPU not present in MD */
543 res = DR_CPU_RES_NOT_IN_MD;
544 stat = DR_CPU_STAT_NOT_PRESENT;
545 } else if (err == -ENODEV) {
546 /* CPU did not call in successfully */
547 res = DR_CPU_RES_CPU_NOT_RESPONDING;
550 printk(KERN_INFO PFX "CPU startup failed err=%d\n",
551 err);
552 dr_cpu_mark(resp, cpu, ncpus, res, stat);
556 spin_lock_irqsave(&ds_lock, flags);
557 ds_send(ds_info->lp, resp, resp_len);
558 spin_unlock_irqrestore(&ds_lock, flags);
560 kfree(resp);
562 return 0;
565 static int dr_cpu_unconfigure(struct ds_cap_state *cp, u64 req_num,
566 cpumask_t *mask)
568 struct ds_data *resp;
569 int resp_len, ncpus;
571 ncpus = cpus_weight(*mask);
572 resp_len = dr_cpu_size_response(ncpus);
573 resp = kzalloc(resp_len, GFP_KERNEL);
574 if (!resp)
575 return -ENOMEM;
577 dr_cpu_init_response(resp, req_num, cp->handle,
578 resp_len, ncpus, mask,
579 DR_CPU_STAT_UNCONFIGURED);
581 kfree(resp);
583 return -EOPNOTSUPP;
586 static void process_dr_cpu_list(struct ds_cap_state *cp)
588 struct dr_cpu_queue_entry *qp, *tmp;
589 unsigned long flags;
590 LIST_HEAD(todo);
591 cpumask_t mask;
593 spin_lock_irqsave(&ds_lock, flags);
594 list_splice(&dr_cpu_work_list, &todo);
595 INIT_LIST_HEAD(&dr_cpu_work_list);
596 spin_unlock_irqrestore(&ds_lock, flags);
598 list_for_each_entry_safe(qp, tmp, &todo, list) {
599 struct ds_data *data = (struct ds_data *) qp->req;
600 struct dr_cpu_tag *tag = (struct dr_cpu_tag *) (data + 1);
601 u32 *cpu_list = (u32 *) (tag + 1);
602 u64 req_num = tag->req_num;
603 unsigned int i;
604 int err;
606 switch (tag->type) {
607 case DR_CPU_CONFIGURE:
608 case DR_CPU_UNCONFIGURE:
609 case DR_CPU_FORCE_UNCONFIGURE:
610 break;
612 default:
613 dr_cpu_send_error(cp, data);
614 goto next;
617 purge_dups(cpu_list, tag->num_records);
619 cpus_clear(mask);
620 for (i = 0; i < tag->num_records; i++) {
621 if (cpu_list[i] == CPU_SENTINEL)
622 continue;
624 if (cpu_list[i] < NR_CPUS)
625 cpu_set(cpu_list[i], mask);
628 if (tag->type == DR_CPU_CONFIGURE)
629 err = dr_cpu_configure(cp, req_num, &mask);
630 else
631 err = dr_cpu_unconfigure(cp, req_num, &mask);
633 if (err)
634 dr_cpu_send_error(cp, data);
636 next:
637 list_del(&qp->list);
638 kfree(qp);
642 static int dr_cpu_thread(void *__unused)
644 struct ds_cap_state *cp;
645 DEFINE_WAIT(wait);
647 cp = find_cap_by_string("dr-cpu");
649 while (1) {
650 prepare_to_wait(&dr_cpu_wait, &wait, TASK_INTERRUPTIBLE);
651 if (list_empty(&dr_cpu_work_list))
652 schedule();
653 finish_wait(&dr_cpu_wait, &wait);
655 if (kthread_should_stop())
656 break;
658 process_dr_cpu_list(cp);
661 return 0;
664 static void dr_cpu_data(struct ldc_channel *lp,
665 struct ds_cap_state *dp,
666 void *buf, int len)
668 struct dr_cpu_queue_entry *qp;
669 struct ds_data *dpkt = buf;
670 struct dr_cpu_tag *rp;
672 rp = (struct dr_cpu_tag *) (dpkt + 1);
674 qp = kmalloc(sizeof(struct dr_cpu_queue_entry) + len, GFP_ATOMIC);
675 if (!qp) {
676 struct ds_cap_state *cp;
678 cp = find_cap_by_string("dr-cpu");
679 __dr_cpu_send_error(cp, dpkt);
680 } else {
681 memcpy(&qp->req, buf, len);
682 list_add_tail(&qp->list, &dr_cpu_work_list);
683 wake_up(&dr_cpu_wait);
686 #endif
688 struct ds_pri_msg {
689 __u64 req_num;
690 __u64 type;
691 #define DS_PRI_REQUEST 0x00
692 #define DS_PRI_DATA 0x01
693 #define DS_PRI_UPDATE 0x02
696 static void ds_pri_data(struct ldc_channel *lp,
697 struct ds_cap_state *dp,
698 void *buf, int len)
700 struct ds_data *dpkt = buf;
701 struct ds_pri_msg *rp;
703 rp = (struct ds_pri_msg *) (dpkt + 1);
705 printk(KERN_INFO PFX "PRI REQ [%lx:%lx], len=%d\n",
706 rp->req_num, rp->type, len);
709 struct ds_var_hdr {
710 __u32 type;
711 #define DS_VAR_SET_REQ 0x00
712 #define DS_VAR_DELETE_REQ 0x01
713 #define DS_VAR_SET_RESP 0x02
714 #define DS_VAR_DELETE_RESP 0x03
717 struct ds_var_set_msg {
718 struct ds_var_hdr hdr;
719 char name_and_value[0];
722 struct ds_var_delete_msg {
723 struct ds_var_hdr hdr;
724 char name[0];
727 struct ds_var_resp {
728 struct ds_var_hdr hdr;
729 __u32 result;
730 #define DS_VAR_SUCCESS 0x00
731 #define DS_VAR_NO_SPACE 0x01
732 #define DS_VAR_INVALID_VAR 0x02
733 #define DS_VAR_INVALID_VAL 0x03
734 #define DS_VAR_NOT_PRESENT 0x04
737 static DEFINE_MUTEX(ds_var_mutex);
738 static int ds_var_doorbell;
739 static int ds_var_response;
741 static void ds_var_data(struct ldc_channel *lp,
742 struct ds_cap_state *dp,
743 void *buf, int len)
745 struct ds_data *dpkt = buf;
746 struct ds_var_resp *rp;
748 rp = (struct ds_var_resp *) (dpkt + 1);
750 if (rp->hdr.type != DS_VAR_SET_RESP &&
751 rp->hdr.type != DS_VAR_DELETE_RESP)
752 return;
754 ds_var_response = rp->result;
755 wmb();
756 ds_var_doorbell = 1;
759 void ldom_set_var(const char *var, const char *value)
761 struct ds_info *dp = ds_info;
762 struct ds_cap_state *cp;
764 cp = find_cap_by_string("var-config");
765 if (cp->state != CAP_STATE_REGISTERED)
766 cp = find_cap_by_string("var-config-backup");
768 if (cp->state == CAP_STATE_REGISTERED) {
769 union {
770 struct {
771 struct ds_data data;
772 struct ds_var_set_msg msg;
773 } header;
774 char all[512];
775 } pkt;
776 unsigned long flags;
777 char *base, *p;
778 int msg_len, loops;
780 memset(&pkt, 0, sizeof(pkt));
781 pkt.header.data.tag.type = DS_DATA;
782 pkt.header.data.handle = cp->handle;
783 pkt.header.msg.hdr.type = DS_VAR_SET_REQ;
784 base = p = &pkt.header.msg.name_and_value[0];
785 strcpy(p, var);
786 p += strlen(var) + 1;
787 strcpy(p, value);
788 p += strlen(value) + 1;
790 msg_len = (sizeof(struct ds_data) +
791 sizeof(struct ds_var_set_msg) +
792 (p - base));
793 msg_len = (msg_len + 3) & ~3;
794 pkt.header.data.tag.len = msg_len - sizeof(struct ds_msg_tag);
796 mutex_lock(&ds_var_mutex);
798 spin_lock_irqsave(&ds_lock, flags);
799 ds_var_doorbell = 0;
800 ds_var_response = -1;
802 ds_send(dp->lp, &pkt, msg_len);
803 spin_unlock_irqrestore(&ds_lock, flags);
805 loops = 1000;
806 while (ds_var_doorbell == 0) {
807 if (loops-- < 0)
808 break;
809 barrier();
810 udelay(100);
813 mutex_unlock(&ds_var_mutex);
815 if (ds_var_doorbell == 0 ||
816 ds_var_response != DS_VAR_SUCCESS)
817 printk(KERN_ERR PFX "var-config [%s:%s] "
818 "failed, response(%d).\n",
819 var, value,
820 ds_var_response);
821 } else {
822 printk(KERN_ERR PFX "var-config not registered so "
823 "could not set (%s) variable to (%s).\n",
824 var, value);
828 void ldom_reboot(const char *boot_command)
830 /* Don't bother with any of this if the boot_command
831 * is empty.
833 if (boot_command && strlen(boot_command)) {
834 char full_boot_str[256];
836 strcpy(full_boot_str, "boot ");
837 strcpy(full_boot_str + strlen("boot "), boot_command);
839 ldom_set_var("reboot-command", full_boot_str);
841 sun4v_mach_sir();
844 void ldom_power_off(void)
846 sun4v_mach_exit(0);
849 static void ds_conn_reset(struct ds_info *dp)
851 printk(KERN_ERR PFX "ds_conn_reset() from %p\n",
852 __builtin_return_address(0));
855 static int register_services(struct ds_info *dp)
857 struct ldc_channel *lp = dp->lp;
858 int i;
860 for (i = 0; i < ARRAY_SIZE(ds_states); i++) {
861 struct {
862 struct ds_reg_req req;
863 u8 id_buf[256];
864 } pbuf;
865 struct ds_cap_state *cp = &ds_states[i];
866 int err, msg_len;
867 u64 new_count;
869 if (cp->state == CAP_STATE_REGISTERED)
870 continue;
872 new_count = sched_clock() & 0xffffffff;
873 cp->handle = ((u64) i << 32) | new_count;
875 msg_len = (sizeof(struct ds_reg_req) +
876 strlen(cp->service_id));
878 memset(&pbuf, 0, sizeof(pbuf));
879 pbuf.req.tag.type = DS_REG_REQ;
880 pbuf.req.tag.len = (msg_len - sizeof(struct ds_msg_tag));
881 pbuf.req.handle = cp->handle;
882 pbuf.req.major = 1;
883 pbuf.req.minor = 0;
884 strcpy(pbuf.req.svc_id, cp->service_id);
886 err = ds_send(lp, &pbuf, msg_len);
887 if (err > 0)
888 cp->state = CAP_STATE_REG_SENT;
890 return 0;
893 static int ds_handshake(struct ds_info *dp, struct ds_msg_tag *pkt)
896 if (dp->hs_state == DS_HS_START) {
897 if (pkt->type != DS_INIT_ACK)
898 goto conn_reset;
900 dp->hs_state = DS_HS_DONE;
902 return register_services(dp);
905 if (dp->hs_state != DS_HS_DONE)
906 goto conn_reset;
908 if (pkt->type == DS_REG_ACK) {
909 struct ds_reg_ack *ap = (struct ds_reg_ack *) pkt;
910 struct ds_cap_state *cp = find_cap(ap->handle);
912 if (!cp) {
913 printk(KERN_ERR PFX "REG ACK for unknown handle %lx\n",
914 ap->handle);
915 return 0;
917 printk(KERN_INFO PFX "Registered %s service.\n",
918 cp->service_id);
919 cp->state = CAP_STATE_REGISTERED;
920 } else if (pkt->type == DS_REG_NACK) {
921 struct ds_reg_nack *np = (struct ds_reg_nack *) pkt;
922 struct ds_cap_state *cp = find_cap(np->handle);
924 if (!cp) {
925 printk(KERN_ERR PFX "REG NACK for "
926 "unknown handle %lx\n",
927 np->handle);
928 return 0;
930 printk(KERN_INFO PFX "Could not register %s service\n",
931 cp->service_id);
932 cp->state = CAP_STATE_UNKNOWN;
935 return 0;
937 conn_reset:
938 ds_conn_reset(dp);
939 return -ECONNRESET;
942 static int ds_data(struct ds_info *dp, struct ds_msg_tag *pkt, int len)
944 struct ds_data *dpkt = (struct ds_data *) pkt;
945 struct ds_cap_state *cp = find_cap(dpkt->handle);
947 if (!cp) {
948 struct ds_data_nack nack = {
949 .tag = {
950 .type = DS_NACK,
951 .len = (sizeof(struct ds_data_nack) -
952 sizeof(struct ds_msg_tag)),
954 .handle = dpkt->handle,
955 .result = DS_INV_HDL,
958 printk(KERN_ERR PFX "Data for unknown handle %lu\n",
959 dpkt->handle);
960 ds_send(dp->lp, &nack, sizeof(nack));
961 } else {
962 cp->data(dp->lp, cp, dpkt, len);
964 return 0;
967 static void ds_up(struct ds_info *dp)
969 struct ldc_channel *lp = dp->lp;
970 struct ds_ver_req req;
971 int err;
973 req.tag.type = DS_INIT_REQ;
974 req.tag.len = sizeof(req) - sizeof(struct ds_msg_tag);
975 req.ver.major = 1;
976 req.ver.minor = 0;
978 err = ds_send(lp, &req, sizeof(req));
979 if (err > 0)
980 dp->hs_state = DS_HS_START;
983 static void ds_event(void *arg, int event)
985 struct ds_info *dp = arg;
986 struct ldc_channel *lp = dp->lp;
987 unsigned long flags;
988 int err;
990 spin_lock_irqsave(&ds_lock, flags);
992 if (event == LDC_EVENT_UP) {
993 ds_up(dp);
994 spin_unlock_irqrestore(&ds_lock, flags);
995 return;
998 if (event != LDC_EVENT_DATA_READY) {
999 printk(KERN_WARNING PFX "Unexpected LDC event %d\n", event);
1000 spin_unlock_irqrestore(&ds_lock, flags);
1001 return;
1004 err = 0;
1005 while (1) {
1006 struct ds_msg_tag *tag;
1008 err = ldc_read(lp, dp->rcv_buf, sizeof(*tag));
1010 if (unlikely(err < 0)) {
1011 if (err == -ECONNRESET)
1012 ds_conn_reset(dp);
1013 break;
1015 if (err == 0)
1016 break;
1018 tag = dp->rcv_buf;
1019 err = ldc_read(lp, tag + 1, tag->len);
1021 if (unlikely(err < 0)) {
1022 if (err == -ECONNRESET)
1023 ds_conn_reset(dp);
1024 break;
1026 if (err < tag->len)
1027 break;
1029 if (tag->type < DS_DATA)
1030 err = ds_handshake(dp, dp->rcv_buf);
1031 else
1032 err = ds_data(dp, dp->rcv_buf,
1033 sizeof(*tag) + err);
1034 if (err == -ECONNRESET)
1035 break;
1038 spin_unlock_irqrestore(&ds_lock, flags);
1041 static int __devinit ds_probe(struct vio_dev *vdev,
1042 const struct vio_device_id *id)
1044 static int ds_version_printed;
1045 struct ldc_channel_config ds_cfg = {
1046 .event = ds_event,
1047 .mtu = 4096,
1048 .mode = LDC_MODE_STREAM,
1050 struct ldc_channel *lp;
1051 struct ds_info *dp;
1052 int err;
1054 if (ds_version_printed++ == 0)
1055 printk(KERN_INFO "%s", version);
1057 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1058 err = -ENOMEM;
1059 if (!dp)
1060 goto out_err;
1062 dp->rcv_buf = kzalloc(4096, GFP_KERNEL);
1063 if (!dp->rcv_buf)
1064 goto out_free_dp;
1066 dp->rcv_buf_len = 4096;
1068 ds_cfg.tx_irq = vdev->tx_irq;
1069 ds_cfg.rx_irq = vdev->rx_irq;
1071 lp = ldc_alloc(vdev->channel_id, &ds_cfg, dp);
1072 if (IS_ERR(lp)) {
1073 err = PTR_ERR(lp);
1074 goto out_free_rcv_buf;
1076 dp->lp = lp;
1078 err = ldc_bind(lp, "DS");
1079 if (err)
1080 goto out_free_ldc;
1082 ds_info = dp;
1084 start_powerd();
1086 return err;
1088 out_free_ldc:
1089 ldc_free(dp->lp);
1091 out_free_rcv_buf:
1092 kfree(dp->rcv_buf);
1094 out_free_dp:
1095 kfree(dp);
1097 out_err:
1098 return err;
1101 static int ds_remove(struct vio_dev *vdev)
1103 return 0;
1106 static struct vio_device_id ds_match[] = {
1108 .type = "domain-services-port",
1113 static struct vio_driver ds_driver = {
1114 .id_table = ds_match,
1115 .probe = ds_probe,
1116 .remove = ds_remove,
1117 .driver = {
1118 .name = "ds",
1119 .owner = THIS_MODULE,
1123 static int __init ds_init(void)
1125 int i;
1127 for (i = 0; i < ARRAY_SIZE(ds_states); i++)
1128 ds_states[i].handle = ((u64)i << 32);
1130 #ifdef CONFIG_HOTPLUG_CPU
1131 kthread_run(dr_cpu_thread, NULL, "kdrcpud");
1132 #endif
1134 return vio_register_driver(&ds_driver);
1137 subsys_initcall(ds_init);