Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / arch / sparc64 / kernel / ds.c
blob9552811653b8adc31a1efef6eeec9171681b3a95
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/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/ldc.h>
19 #include <asm/vio.h>
20 #include <asm/mdesc.h>
21 #include <asm/head.h>
22 #include <asm/irq.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_info;
127 struct ds_cap_state {
128 __u64 handle;
130 void (*data)(struct ds_info *dp,
131 struct ds_cap_state *cp,
132 void *buf, int len);
134 const char *service_id;
136 u8 state;
137 #define CAP_STATE_UNKNOWN 0x00
138 #define CAP_STATE_REG_SENT 0x01
139 #define CAP_STATE_REGISTERED 0x02
142 static void md_update_data(struct ds_info *dp, struct ds_cap_state *cp,
143 void *buf, int len);
144 static void domain_shutdown_data(struct ds_info *dp,
145 struct ds_cap_state *cp,
146 void *buf, int len);
147 static void domain_panic_data(struct ds_info *dp,
148 struct ds_cap_state *cp,
149 void *buf, int len);
150 #ifdef CONFIG_HOTPLUG_CPU
151 static void dr_cpu_data(struct ds_info *dp,
152 struct ds_cap_state *cp,
153 void *buf, int len);
154 #endif
155 static void ds_pri_data(struct ds_info *dp,
156 struct ds_cap_state *cp,
157 void *buf, int len);
158 static void ds_var_data(struct ds_info *dp,
159 struct ds_cap_state *cp,
160 void *buf, int len);
162 struct ds_cap_state ds_states_template[] = {
164 .service_id = "md-update",
165 .data = md_update_data,
168 .service_id = "domain-shutdown",
169 .data = domain_shutdown_data,
172 .service_id = "domain-panic",
173 .data = domain_panic_data,
175 #ifdef CONFIG_HOTPLUG_CPU
177 .service_id = "dr-cpu",
178 .data = dr_cpu_data,
180 #endif
182 .service_id = "pri",
183 .data = ds_pri_data,
186 .service_id = "var-config",
187 .data = ds_var_data,
190 .service_id = "var-config-backup",
191 .data = ds_var_data,
195 static DEFINE_SPINLOCK(ds_lock);
197 struct ds_info {
198 struct ldc_channel *lp;
199 u8 hs_state;
200 #define DS_HS_START 0x01
201 #define DS_HS_DONE 0x02
203 u64 id;
205 void *rcv_buf;
206 int rcv_buf_len;
208 struct ds_cap_state *ds_states;
209 int num_ds_states;
211 struct ds_info *next;
214 static struct ds_info *ds_info_list;
216 static struct ds_cap_state *find_cap(struct ds_info *dp, u64 handle)
218 unsigned int index = handle >> 32;
220 if (index >= dp->num_ds_states)
221 return NULL;
222 return &dp->ds_states[index];
225 static struct ds_cap_state *find_cap_by_string(struct ds_info *dp,
226 const char *name)
228 int i;
230 for (i = 0; i < dp->num_ds_states; i++) {
231 if (strcmp(dp->ds_states[i].service_id, name))
232 continue;
234 return &dp->ds_states[i];
236 return NULL;
239 static int __ds_send(struct ldc_channel *lp, void *data, int len)
241 int err, limit = 1000;
243 err = -EINVAL;
244 while (limit-- > 0) {
245 err = ldc_write(lp, data, len);
246 if (!err || (err != -EAGAIN))
247 break;
248 udelay(1);
251 return err;
254 static int ds_send(struct ldc_channel *lp, void *data, int len)
256 unsigned long flags;
257 int err;
259 spin_lock_irqsave(&ds_lock, flags);
260 err = __ds_send(lp, data, len);
261 spin_unlock_irqrestore(&ds_lock, flags);
263 return err;
266 struct ds_md_update_req {
267 __u64 req_num;
270 struct ds_md_update_res {
271 __u64 req_num;
272 __u32 result;
275 static void md_update_data(struct ds_info *dp,
276 struct ds_cap_state *cp,
277 void *buf, int len)
279 struct ldc_channel *lp = dp->lp;
280 struct ds_data *dpkt = buf;
281 struct ds_md_update_req *rp;
282 struct {
283 struct ds_data data;
284 struct ds_md_update_res res;
285 } pkt;
287 rp = (struct ds_md_update_req *) (dpkt + 1);
289 printk(KERN_INFO "ds-%lu: Machine description update.\n", dp->id);
291 mdesc_update();
293 memset(&pkt, 0, sizeof(pkt));
294 pkt.data.tag.type = DS_DATA;
295 pkt.data.tag.len = sizeof(pkt) - sizeof(struct ds_msg_tag);
296 pkt.data.handle = cp->handle;
297 pkt.res.req_num = rp->req_num;
298 pkt.res.result = DS_OK;
300 ds_send(lp, &pkt, sizeof(pkt));
303 struct ds_shutdown_req {
304 __u64 req_num;
305 __u32 ms_delay;
308 struct ds_shutdown_res {
309 __u64 req_num;
310 __u32 result;
311 char reason[1];
314 static void domain_shutdown_data(struct ds_info *dp,
315 struct ds_cap_state *cp,
316 void *buf, int len)
318 struct ldc_channel *lp = dp->lp;
319 struct ds_data *dpkt = buf;
320 struct ds_shutdown_req *rp;
321 struct {
322 struct ds_data data;
323 struct ds_shutdown_res res;
324 } pkt;
326 rp = (struct ds_shutdown_req *) (dpkt + 1);
328 printk(KERN_ALERT "ds-%lu: Shutdown request from "
329 "LDOM manager received.\n", dp->id);
331 memset(&pkt, 0, sizeof(pkt));
332 pkt.data.tag.type = DS_DATA;
333 pkt.data.tag.len = sizeof(pkt) - sizeof(struct ds_msg_tag);
334 pkt.data.handle = cp->handle;
335 pkt.res.req_num = rp->req_num;
336 pkt.res.result = DS_OK;
337 pkt.res.reason[0] = 0;
339 ds_send(lp, &pkt, sizeof(pkt));
341 orderly_poweroff(true);
344 struct ds_panic_req {
345 __u64 req_num;
348 struct ds_panic_res {
349 __u64 req_num;
350 __u32 result;
351 char reason[1];
354 static void domain_panic_data(struct ds_info *dp,
355 struct ds_cap_state *cp,
356 void *buf, int len)
358 struct ldc_channel *lp = dp->lp;
359 struct ds_data *dpkt = buf;
360 struct ds_panic_req *rp;
361 struct {
362 struct ds_data data;
363 struct ds_panic_res res;
364 } pkt;
366 rp = (struct ds_panic_req *) (dpkt + 1);
368 printk(KERN_ALERT "ds-%lu: Panic request from "
369 "LDOM manager received.\n", dp->id);
371 memset(&pkt, 0, sizeof(pkt));
372 pkt.data.tag.type = DS_DATA;
373 pkt.data.tag.len = sizeof(pkt) - sizeof(struct ds_msg_tag);
374 pkt.data.handle = cp->handle;
375 pkt.res.req_num = rp->req_num;
376 pkt.res.result = DS_OK;
377 pkt.res.reason[0] = 0;
379 ds_send(lp, &pkt, sizeof(pkt));
381 panic("PANIC requested by LDOM manager.");
384 #ifdef CONFIG_HOTPLUG_CPU
385 struct dr_cpu_tag {
386 __u64 req_num;
387 __u32 type;
388 #define DR_CPU_CONFIGURE 0x43
389 #define DR_CPU_UNCONFIGURE 0x55
390 #define DR_CPU_FORCE_UNCONFIGURE 0x46
391 #define DR_CPU_STATUS 0x53
393 /* Responses */
394 #define DR_CPU_OK 0x6f
395 #define DR_CPU_ERROR 0x65
397 __u32 num_records;
400 struct dr_cpu_resp_entry {
401 __u32 cpu;
402 __u32 result;
403 #define DR_CPU_RES_OK 0x00
404 #define DR_CPU_RES_FAILURE 0x01
405 #define DR_CPU_RES_BLOCKED 0x02
406 #define DR_CPU_RES_CPU_NOT_RESPONDING 0x03
407 #define DR_CPU_RES_NOT_IN_MD 0x04
409 __u32 stat;
410 #define DR_CPU_STAT_NOT_PRESENT 0x00
411 #define DR_CPU_STAT_UNCONFIGURED 0x01
412 #define DR_CPU_STAT_CONFIGURED 0x02
414 __u32 str_off;
417 static void __dr_cpu_send_error(struct ds_info *dp,
418 struct ds_cap_state *cp,
419 struct ds_data *data)
421 struct dr_cpu_tag *tag = (struct dr_cpu_tag *) (data + 1);
422 struct {
423 struct ds_data data;
424 struct dr_cpu_tag tag;
425 } pkt;
426 int msg_len;
428 memset(&pkt, 0, sizeof(pkt));
429 pkt.data.tag.type = DS_DATA;
430 pkt.data.handle = cp->handle;
431 pkt.tag.req_num = tag->req_num;
432 pkt.tag.type = DR_CPU_ERROR;
433 pkt.tag.num_records = 0;
435 msg_len = (sizeof(struct ds_data) +
436 sizeof(struct dr_cpu_tag));
438 pkt.data.tag.len = msg_len - sizeof(struct ds_msg_tag);
440 __ds_send(dp->lp, &pkt, msg_len);
443 static void dr_cpu_send_error(struct ds_info *dp,
444 struct ds_cap_state *cp,
445 struct ds_data *data)
447 unsigned long flags;
449 spin_lock_irqsave(&ds_lock, flags);
450 __dr_cpu_send_error(dp, cp, data);
451 spin_unlock_irqrestore(&ds_lock, flags);
454 #define CPU_SENTINEL 0xffffffff
456 static void purge_dups(u32 *list, u32 num_ents)
458 unsigned int i;
460 for (i = 0; i < num_ents; i++) {
461 u32 cpu = list[i];
462 unsigned int j;
464 if (cpu == CPU_SENTINEL)
465 continue;
467 for (j = i + 1; j < num_ents; j++) {
468 if (list[j] == cpu)
469 list[j] = CPU_SENTINEL;
474 static int dr_cpu_size_response(int ncpus)
476 return (sizeof(struct ds_data) +
477 sizeof(struct dr_cpu_tag) +
478 (sizeof(struct dr_cpu_resp_entry) * ncpus));
481 static void dr_cpu_init_response(struct ds_data *resp, u64 req_num,
482 u64 handle, int resp_len, int ncpus,
483 cpumask_t *mask, u32 default_stat)
485 struct dr_cpu_resp_entry *ent;
486 struct dr_cpu_tag *tag;
487 int i, cpu;
489 tag = (struct dr_cpu_tag *) (resp + 1);
490 ent = (struct dr_cpu_resp_entry *) (tag + 1);
492 resp->tag.type = DS_DATA;
493 resp->tag.len = resp_len - sizeof(struct ds_msg_tag);
494 resp->handle = handle;
495 tag->req_num = req_num;
496 tag->type = DR_CPU_OK;
497 tag->num_records = ncpus;
499 i = 0;
500 for_each_cpu_mask(cpu, *mask) {
501 ent[i].cpu = cpu;
502 ent[i].result = DR_CPU_RES_OK;
503 ent[i].stat = default_stat;
504 i++;
506 BUG_ON(i != ncpus);
509 static void dr_cpu_mark(struct ds_data *resp, int cpu, int ncpus,
510 u32 res, u32 stat)
512 struct dr_cpu_resp_entry *ent;
513 struct dr_cpu_tag *tag;
514 int i;
516 tag = (struct dr_cpu_tag *) (resp + 1);
517 ent = (struct dr_cpu_resp_entry *) (tag + 1);
519 for (i = 0; i < ncpus; i++) {
520 if (ent[i].cpu != cpu)
521 continue;
522 ent[i].result = res;
523 ent[i].stat = stat;
524 break;
528 <<<<<<< HEAD:arch/sparc64/kernel/ds.c
529 static int dr_cpu_configure(struct ds_info *dp,
530 struct ds_cap_state *cp,
531 u64 req_num,
532 cpumask_t *mask)
533 =======
534 static int __cpuinit dr_cpu_configure(struct ds_info *dp,
535 struct ds_cap_state *cp,
536 u64 req_num,
537 cpumask_t *mask)
538 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/sparc64/kernel/ds.c
540 struct ds_data *resp;
541 int resp_len, ncpus, cpu;
542 unsigned long flags;
544 ncpus = cpus_weight(*mask);
545 resp_len = dr_cpu_size_response(ncpus);
546 resp = kzalloc(resp_len, GFP_KERNEL);
547 if (!resp)
548 return -ENOMEM;
550 dr_cpu_init_response(resp, req_num, cp->handle,
551 resp_len, ncpus, mask,
552 DR_CPU_STAT_CONFIGURED);
554 mdesc_fill_in_cpu_data(*mask);
556 for_each_cpu_mask(cpu, *mask) {
557 int err;
559 printk(KERN_INFO "ds-%lu: Starting cpu %d...\n",
560 dp->id, cpu);
561 err = cpu_up(cpu);
562 if (err) {
563 __u32 res = DR_CPU_RES_FAILURE;
564 __u32 stat = DR_CPU_STAT_UNCONFIGURED;
566 if (!cpu_present(cpu)) {
567 /* CPU not present in MD */
568 res = DR_CPU_RES_NOT_IN_MD;
569 stat = DR_CPU_STAT_NOT_PRESENT;
570 } else if (err == -ENODEV) {
571 /* CPU did not call in successfully */
572 res = DR_CPU_RES_CPU_NOT_RESPONDING;
575 printk(KERN_INFO "ds-%lu: CPU startup failed err=%d\n",
576 dp->id, err);
577 dr_cpu_mark(resp, cpu, ncpus, res, stat);
581 spin_lock_irqsave(&ds_lock, flags);
582 __ds_send(dp->lp, resp, resp_len);
583 spin_unlock_irqrestore(&ds_lock, flags);
585 kfree(resp);
587 /* Redistribute IRQs, taking into account the new cpus. */
588 fixup_irqs();
590 return 0;
593 static int dr_cpu_unconfigure(struct ds_info *dp,
594 struct ds_cap_state *cp,
595 u64 req_num,
596 cpumask_t *mask)
598 struct ds_data *resp;
599 int resp_len, ncpus, cpu;
600 unsigned long flags;
602 ncpus = cpus_weight(*mask);
603 resp_len = dr_cpu_size_response(ncpus);
604 resp = kzalloc(resp_len, GFP_KERNEL);
605 if (!resp)
606 return -ENOMEM;
608 dr_cpu_init_response(resp, req_num, cp->handle,
609 resp_len, ncpus, mask,
610 DR_CPU_STAT_UNCONFIGURED);
612 for_each_cpu_mask(cpu, *mask) {
613 int err;
615 printk(KERN_INFO "ds-%lu: Shutting down cpu %d...\n",
616 dp->id, cpu);
617 err = cpu_down(cpu);
618 if (err)
619 dr_cpu_mark(resp, cpu, ncpus,
620 DR_CPU_RES_FAILURE,
621 DR_CPU_STAT_CONFIGURED);
624 spin_lock_irqsave(&ds_lock, flags);
625 __ds_send(dp->lp, resp, resp_len);
626 spin_unlock_irqrestore(&ds_lock, flags);
628 kfree(resp);
630 return 0;
633 <<<<<<< HEAD:arch/sparc64/kernel/ds.c
634 static void dr_cpu_data(struct ds_info *dp,
635 struct ds_cap_state *cp,
636 void *buf, int len)
637 =======
638 static void __cpuinit dr_cpu_data(struct ds_info *dp,
639 struct ds_cap_state *cp,
640 void *buf, int len)
641 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/sparc64/kernel/ds.c
643 struct ds_data *data = buf;
644 struct dr_cpu_tag *tag = (struct dr_cpu_tag *) (data + 1);
645 u32 *cpu_list = (u32 *) (tag + 1);
646 u64 req_num = tag->req_num;
647 cpumask_t mask;
648 unsigned int i;
649 int err;
651 switch (tag->type) {
652 case DR_CPU_CONFIGURE:
653 case DR_CPU_UNCONFIGURE:
654 case DR_CPU_FORCE_UNCONFIGURE:
655 break;
657 default:
658 dr_cpu_send_error(dp, cp, data);
659 return;
662 purge_dups(cpu_list, tag->num_records);
664 cpus_clear(mask);
665 for (i = 0; i < tag->num_records; i++) {
666 if (cpu_list[i] == CPU_SENTINEL)
667 continue;
669 if (cpu_list[i] < NR_CPUS)
670 cpu_set(cpu_list[i], mask);
673 if (tag->type == DR_CPU_CONFIGURE)
674 err = dr_cpu_configure(dp, cp, req_num, &mask);
675 else
676 err = dr_cpu_unconfigure(dp, cp, req_num, &mask);
678 if (err)
679 dr_cpu_send_error(dp, cp, data);
681 #endif /* CONFIG_HOTPLUG_CPU */
683 struct ds_pri_msg {
684 __u64 req_num;
685 __u64 type;
686 #define DS_PRI_REQUEST 0x00
687 #define DS_PRI_DATA 0x01
688 #define DS_PRI_UPDATE 0x02
691 static void ds_pri_data(struct ds_info *dp,
692 struct ds_cap_state *cp,
693 void *buf, int len)
695 struct ds_data *dpkt = buf;
696 struct ds_pri_msg *rp;
698 rp = (struct ds_pri_msg *) (dpkt + 1);
700 printk(KERN_INFO "ds-%lu: PRI REQ [%lx:%lx], len=%d\n",
701 dp->id, rp->req_num, rp->type, len);
704 struct ds_var_hdr {
705 __u32 type;
706 #define DS_VAR_SET_REQ 0x00
707 #define DS_VAR_DELETE_REQ 0x01
708 #define DS_VAR_SET_RESP 0x02
709 #define DS_VAR_DELETE_RESP 0x03
712 struct ds_var_set_msg {
713 struct ds_var_hdr hdr;
714 char name_and_value[0];
717 struct ds_var_delete_msg {
718 struct ds_var_hdr hdr;
719 char name[0];
722 struct ds_var_resp {
723 struct ds_var_hdr hdr;
724 __u32 result;
725 #define DS_VAR_SUCCESS 0x00
726 #define DS_VAR_NO_SPACE 0x01
727 #define DS_VAR_INVALID_VAR 0x02
728 #define DS_VAR_INVALID_VAL 0x03
729 #define DS_VAR_NOT_PRESENT 0x04
732 static DEFINE_MUTEX(ds_var_mutex);
733 static int ds_var_doorbell;
734 static int ds_var_response;
736 static void ds_var_data(struct ds_info *dp,
737 struct ds_cap_state *cp,
738 void *buf, int len)
740 struct ds_data *dpkt = buf;
741 struct ds_var_resp *rp;
743 rp = (struct ds_var_resp *) (dpkt + 1);
745 if (rp->hdr.type != DS_VAR_SET_RESP &&
746 rp->hdr.type != DS_VAR_DELETE_RESP)
747 return;
749 ds_var_response = rp->result;
750 wmb();
751 ds_var_doorbell = 1;
754 void ldom_set_var(const char *var, const char *value)
756 struct ds_cap_state *cp;
757 struct ds_info *dp;
758 unsigned long flags;
760 spin_lock_irqsave(&ds_lock, flags);
761 cp = NULL;
762 for (dp = ds_info_list; dp; dp = dp->next) {
763 struct ds_cap_state *tmp;
765 tmp = find_cap_by_string(dp, "var-config");
766 if (tmp && tmp->state == CAP_STATE_REGISTERED) {
767 cp = tmp;
768 break;
771 if (!cp) {
772 for (dp = ds_info_list; dp; dp = dp->next) {
773 struct ds_cap_state *tmp;
775 tmp = find_cap_by_string(dp, "var-config-backup");
776 if (tmp && tmp->state == CAP_STATE_REGISTERED) {
777 cp = tmp;
778 break;
782 spin_unlock_irqrestore(&ds_lock, flags);
784 if (cp) {
785 union {
786 struct {
787 struct ds_data data;
788 struct ds_var_set_msg msg;
789 } header;
790 char all[512];
791 } pkt;
792 char *base, *p;
793 int msg_len, loops;
795 memset(&pkt, 0, sizeof(pkt));
796 pkt.header.data.tag.type = DS_DATA;
797 pkt.header.data.handle = cp->handle;
798 pkt.header.msg.hdr.type = DS_VAR_SET_REQ;
799 base = p = &pkt.header.msg.name_and_value[0];
800 strcpy(p, var);
801 p += strlen(var) + 1;
802 strcpy(p, value);
803 p += strlen(value) + 1;
805 msg_len = (sizeof(struct ds_data) +
806 sizeof(struct ds_var_set_msg) +
807 (p - base));
808 msg_len = (msg_len + 3) & ~3;
809 pkt.header.data.tag.len = msg_len - sizeof(struct ds_msg_tag);
811 mutex_lock(&ds_var_mutex);
813 spin_lock_irqsave(&ds_lock, flags);
814 ds_var_doorbell = 0;
815 ds_var_response = -1;
817 __ds_send(dp->lp, &pkt, msg_len);
818 spin_unlock_irqrestore(&ds_lock, flags);
820 loops = 1000;
821 while (ds_var_doorbell == 0) {
822 if (loops-- < 0)
823 break;
824 barrier();
825 udelay(100);
828 mutex_unlock(&ds_var_mutex);
830 if (ds_var_doorbell == 0 ||
831 ds_var_response != DS_VAR_SUCCESS)
832 printk(KERN_ERR "ds-%lu: var-config [%s:%s] "
833 "failed, response(%d).\n",
834 dp->id, var, value,
835 ds_var_response);
836 } else {
837 printk(KERN_ERR PFX "var-config not registered so "
838 "could not set (%s) variable to (%s).\n",
839 var, value);
843 void ldom_reboot(const char *boot_command)
845 /* Don't bother with any of this if the boot_command
846 * is empty.
848 if (boot_command && strlen(boot_command)) {
849 char full_boot_str[256];
851 strcpy(full_boot_str, "boot ");
852 strcpy(full_boot_str + strlen("boot "), boot_command);
854 ldom_set_var("reboot-command", full_boot_str);
856 sun4v_mach_sir();
859 void ldom_power_off(void)
861 sun4v_mach_exit(0);
864 static void ds_conn_reset(struct ds_info *dp)
866 printk(KERN_ERR "ds-%lu: ds_conn_reset() from %p\n",
867 dp->id, __builtin_return_address(0));
870 static int register_services(struct ds_info *dp)
872 struct ldc_channel *lp = dp->lp;
873 int i;
875 for (i = 0; i < dp->num_ds_states; i++) {
876 struct {
877 struct ds_reg_req req;
878 u8 id_buf[256];
879 } pbuf;
880 struct ds_cap_state *cp = &dp->ds_states[i];
881 int err, msg_len;
882 u64 new_count;
884 if (cp->state == CAP_STATE_REGISTERED)
885 continue;
887 new_count = sched_clock() & 0xffffffff;
888 cp->handle = ((u64) i << 32) | new_count;
890 msg_len = (sizeof(struct ds_reg_req) +
891 strlen(cp->service_id));
893 memset(&pbuf, 0, sizeof(pbuf));
894 pbuf.req.tag.type = DS_REG_REQ;
895 pbuf.req.tag.len = (msg_len - sizeof(struct ds_msg_tag));
896 pbuf.req.handle = cp->handle;
897 pbuf.req.major = 1;
898 pbuf.req.minor = 0;
899 strcpy(pbuf.req.svc_id, cp->service_id);
901 err = __ds_send(lp, &pbuf, msg_len);
902 if (err > 0)
903 cp->state = CAP_STATE_REG_SENT;
905 return 0;
908 static int ds_handshake(struct ds_info *dp, struct ds_msg_tag *pkt)
911 if (dp->hs_state == DS_HS_START) {
912 if (pkt->type != DS_INIT_ACK)
913 goto conn_reset;
915 dp->hs_state = DS_HS_DONE;
917 return register_services(dp);
920 if (dp->hs_state != DS_HS_DONE)
921 goto conn_reset;
923 if (pkt->type == DS_REG_ACK) {
924 struct ds_reg_ack *ap = (struct ds_reg_ack *) pkt;
925 struct ds_cap_state *cp = find_cap(dp, ap->handle);
927 if (!cp) {
928 printk(KERN_ERR "ds-%lu: REG ACK for unknown "
929 "handle %lx\n", dp->id, ap->handle);
930 return 0;
932 printk(KERN_INFO "ds-%lu: Registered %s service.\n",
933 dp->id, cp->service_id);
934 cp->state = CAP_STATE_REGISTERED;
935 } else if (pkt->type == DS_REG_NACK) {
936 struct ds_reg_nack *np = (struct ds_reg_nack *) pkt;
937 struct ds_cap_state *cp = find_cap(dp, np->handle);
939 if (!cp) {
940 printk(KERN_ERR "ds-%lu: REG NACK for "
941 "unknown handle %lx\n",
942 dp->id, np->handle);
943 return 0;
945 cp->state = CAP_STATE_UNKNOWN;
948 return 0;
950 conn_reset:
951 ds_conn_reset(dp);
952 return -ECONNRESET;
955 static void __send_ds_nack(struct ds_info *dp, u64 handle)
957 struct ds_data_nack nack = {
958 .tag = {
959 .type = DS_NACK,
960 .len = (sizeof(struct ds_data_nack) -
961 sizeof(struct ds_msg_tag)),
963 .handle = handle,
964 .result = DS_INV_HDL,
967 __ds_send(dp->lp, &nack, sizeof(nack));
970 static LIST_HEAD(ds_work_list);
971 static DECLARE_WAIT_QUEUE_HEAD(ds_wait);
973 struct ds_queue_entry {
974 struct list_head list;
975 struct ds_info *dp;
976 int req_len;
977 int __pad;
978 u64 req[0];
981 static void process_ds_work(void)
983 struct ds_queue_entry *qp, *tmp;
984 unsigned long flags;
985 LIST_HEAD(todo);
987 spin_lock_irqsave(&ds_lock, flags);
988 list_splice(&ds_work_list, &todo);
989 INIT_LIST_HEAD(&ds_work_list);
990 spin_unlock_irqrestore(&ds_lock, flags);
992 list_for_each_entry_safe(qp, tmp, &todo, list) {
993 struct ds_data *dpkt = (struct ds_data *) qp->req;
994 struct ds_info *dp = qp->dp;
995 struct ds_cap_state *cp = find_cap(dp, dpkt->handle);
996 int req_len = qp->req_len;
998 if (!cp) {
999 printk(KERN_ERR "ds-%lu: Data for unknown "
1000 "handle %lu\n",
1001 dp->id, dpkt->handle);
1003 spin_lock_irqsave(&ds_lock, flags);
1004 __send_ds_nack(dp, dpkt->handle);
1005 spin_unlock_irqrestore(&ds_lock, flags);
1006 } else {
1007 cp->data(dp, cp, dpkt, req_len);
1010 list_del(&qp->list);
1011 kfree(qp);
1015 static int ds_thread(void *__unused)
1017 DEFINE_WAIT(wait);
1019 while (1) {
1020 prepare_to_wait(&ds_wait, &wait, TASK_INTERRUPTIBLE);
1021 if (list_empty(&ds_work_list))
1022 schedule();
1023 finish_wait(&ds_wait, &wait);
1025 if (kthread_should_stop())
1026 break;
1028 process_ds_work();
1031 return 0;
1034 static int ds_data(struct ds_info *dp, struct ds_msg_tag *pkt, int len)
1036 struct ds_data *dpkt = (struct ds_data *) pkt;
1037 struct ds_queue_entry *qp;
1039 qp = kmalloc(sizeof(struct ds_queue_entry) + len, GFP_ATOMIC);
1040 if (!qp) {
1041 __send_ds_nack(dp, dpkt->handle);
1042 } else {
1043 qp->dp = dp;
1044 memcpy(&qp->req, pkt, len);
1045 list_add_tail(&qp->list, &ds_work_list);
1046 wake_up(&ds_wait);
1048 return 0;
1051 static void ds_up(struct ds_info *dp)
1053 struct ldc_channel *lp = dp->lp;
1054 struct ds_ver_req req;
1055 int err;
1057 req.tag.type = DS_INIT_REQ;
1058 req.tag.len = sizeof(req) - sizeof(struct ds_msg_tag);
1059 req.ver.major = 1;
1060 req.ver.minor = 0;
1062 err = __ds_send(lp, &req, sizeof(req));
1063 if (err > 0)
1064 dp->hs_state = DS_HS_START;
1067 static void ds_reset(struct ds_info *dp)
1069 int i;
1071 dp->hs_state = 0;
1073 for (i = 0; i < dp->num_ds_states; i++) {
1074 struct ds_cap_state *cp = &dp->ds_states[i];
1076 cp->state = CAP_STATE_UNKNOWN;
1080 static void ds_event(void *arg, int event)
1082 struct ds_info *dp = arg;
1083 struct ldc_channel *lp = dp->lp;
1084 unsigned long flags;
1085 int err;
1087 spin_lock_irqsave(&ds_lock, flags);
1089 if (event == LDC_EVENT_UP) {
1090 ds_up(dp);
1091 spin_unlock_irqrestore(&ds_lock, flags);
1092 return;
1095 if (event == LDC_EVENT_RESET) {
1096 ds_reset(dp);
1097 spin_unlock_irqrestore(&ds_lock, flags);
1098 return;
1101 if (event != LDC_EVENT_DATA_READY) {
1102 printk(KERN_WARNING "ds-%lu: Unexpected LDC event %d\n",
1103 dp->id, event);
1104 spin_unlock_irqrestore(&ds_lock, flags);
1105 return;
1108 err = 0;
1109 while (1) {
1110 struct ds_msg_tag *tag;
1112 err = ldc_read(lp, dp->rcv_buf, sizeof(*tag));
1114 if (unlikely(err < 0)) {
1115 if (err == -ECONNRESET)
1116 ds_conn_reset(dp);
1117 break;
1119 if (err == 0)
1120 break;
1122 tag = dp->rcv_buf;
1123 err = ldc_read(lp, tag + 1, tag->len);
1125 if (unlikely(err < 0)) {
1126 if (err == -ECONNRESET)
1127 ds_conn_reset(dp);
1128 break;
1130 if (err < tag->len)
1131 break;
1133 if (tag->type < DS_DATA)
1134 err = ds_handshake(dp, dp->rcv_buf);
1135 else
1136 err = ds_data(dp, dp->rcv_buf,
1137 sizeof(*tag) + err);
1138 if (err == -ECONNRESET)
1139 break;
1142 spin_unlock_irqrestore(&ds_lock, flags);
1145 static int __devinit ds_probe(struct vio_dev *vdev,
1146 const struct vio_device_id *id)
1148 static int ds_version_printed;
1149 struct ldc_channel_config ds_cfg = {
1150 .event = ds_event,
1151 .mtu = 4096,
1152 .mode = LDC_MODE_STREAM,
1154 struct mdesc_handle *hp;
1155 struct ldc_channel *lp;
1156 struct ds_info *dp;
1157 const u64 *val;
1158 int err, i;
1160 if (ds_version_printed++ == 0)
1161 printk(KERN_INFO "%s", version);
1163 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1164 err = -ENOMEM;
1165 if (!dp)
1166 goto out_err;
1168 hp = mdesc_grab();
1169 val = mdesc_get_property(hp, vdev->mp, "id", NULL);
1170 if (val)
1171 dp->id = *val;
1172 mdesc_release(hp);
1174 dp->rcv_buf = kzalloc(4096, GFP_KERNEL);
1175 if (!dp->rcv_buf)
1176 goto out_free_dp;
1178 dp->rcv_buf_len = 4096;
1180 dp->ds_states = kzalloc(sizeof(ds_states_template),
1181 GFP_KERNEL);
1182 if (!dp->ds_states)
1183 goto out_free_rcv_buf;
1185 memcpy(dp->ds_states, ds_states_template,
1186 sizeof(ds_states_template));
1187 dp->num_ds_states = ARRAY_SIZE(ds_states_template);
1189 for (i = 0; i < dp->num_ds_states; i++)
1190 dp->ds_states[i].handle = ((u64)i << 32);
1192 ds_cfg.tx_irq = vdev->tx_irq;
1193 ds_cfg.rx_irq = vdev->rx_irq;
1195 lp = ldc_alloc(vdev->channel_id, &ds_cfg, dp);
1196 if (IS_ERR(lp)) {
1197 err = PTR_ERR(lp);
1198 goto out_free_ds_states;
1200 dp->lp = lp;
1202 err = ldc_bind(lp, "DS");
1203 if (err)
1204 goto out_free_ldc;
1206 spin_lock_irq(&ds_lock);
1207 dp->next = ds_info_list;
1208 ds_info_list = dp;
1209 spin_unlock_irq(&ds_lock);
1211 return err;
1213 out_free_ldc:
1214 ldc_free(dp->lp);
1216 out_free_ds_states:
1217 kfree(dp->ds_states);
1219 out_free_rcv_buf:
1220 kfree(dp->rcv_buf);
1222 out_free_dp:
1223 kfree(dp);
1225 out_err:
1226 return err;
1229 static int ds_remove(struct vio_dev *vdev)
1231 return 0;
1234 static struct vio_device_id ds_match[] = {
1236 .type = "domain-services-port",
1241 static struct vio_driver ds_driver = {
1242 .id_table = ds_match,
1243 .probe = ds_probe,
1244 .remove = ds_remove,
1245 .driver = {
1246 .name = "ds",
1247 .owner = THIS_MODULE,
1251 static int __init ds_init(void)
1253 kthread_run(ds_thread, NULL, "kldomd");
1255 return vio_register_driver(&ds_driver);
1258 subsys_initcall(ds_init);