1 /* ds.c: Domain Services driver for Logical Domains
3 * Copyright (C) 2007 David S. Miller <davem@davemloft.net>
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/reboot.h>
17 #include <linux/cpu.h>
21 #include <asm/mdesc.h>
25 #define DRV_MODULE_NAME "ds"
26 #define PFX DRV_MODULE_NAME ": "
27 #define DRV_MODULE_VERSION "1.0"
28 #define DRV_MODULE_RELDATE "Jul 11, 2007"
30 static char version
[] __devinitdata
=
31 DRV_MODULE_NAME
".c:v" DRV_MODULE_VERSION
" (" DRV_MODULE_RELDATE
")\n";
32 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
33 MODULE_DESCRIPTION("Sun LDOM domain services driver");
34 MODULE_LICENSE("GPL");
35 MODULE_VERSION(DRV_MODULE_VERSION
);
39 #define DS_INIT_REQ 0x00
40 #define DS_INIT_ACK 0x01
41 #define DS_INIT_NACK 0x02
42 #define DS_REG_REQ 0x03
43 #define DS_REG_ACK 0x04
44 #define DS_REG_NACK 0x05
45 #define DS_UNREG_REQ 0x06
46 #define DS_UNREG_ACK 0x07
47 #define DS_UNREG_NACK 0x08
56 #define DS_REG_VER_NACK 0x01
57 #define DS_REG_DUP 0x02
58 #define DS_INV_HDL 0x03
59 #define DS_TYPE_UNKNOWN 0x04
67 struct ds_msg_tag tag
;
68 struct ds_version ver
;
72 struct ds_msg_tag tag
;
77 struct ds_msg_tag tag
;
82 struct ds_msg_tag tag
;
90 struct ds_msg_tag tag
;
96 struct ds_msg_tag tag
;
101 struct ds_unreg_req
{
102 struct ds_msg_tag tag
;
106 struct ds_unreg_ack
{
107 struct ds_msg_tag tag
;
111 struct ds_unreg_nack
{
112 struct ds_msg_tag tag
;
117 struct ds_msg_tag tag
;
121 struct ds_data_nack
{
122 struct ds_msg_tag tag
;
128 struct ds_cap_state
{
131 void (*data
)(struct ds_info
*dp
,
132 struct ds_cap_state
*cp
,
135 const char *service_id
;
138 #define CAP_STATE_UNKNOWN 0x00
139 #define CAP_STATE_REG_SENT 0x01
140 #define CAP_STATE_REGISTERED 0x02
143 static void md_update_data(struct ds_info
*dp
, struct ds_cap_state
*cp
,
145 static void domain_shutdown_data(struct ds_info
*dp
,
146 struct ds_cap_state
*cp
,
148 static void domain_panic_data(struct ds_info
*dp
,
149 struct ds_cap_state
*cp
,
151 #ifdef CONFIG_HOTPLUG_CPU
152 static void dr_cpu_data(struct ds_info
*dp
,
153 struct ds_cap_state
*cp
,
156 static void ds_pri_data(struct ds_info
*dp
,
157 struct ds_cap_state
*cp
,
159 static void ds_var_data(struct ds_info
*dp
,
160 struct ds_cap_state
*cp
,
163 struct ds_cap_state ds_states_template
[] = {
165 .service_id
= "md-update",
166 .data
= md_update_data
,
169 .service_id
= "domain-shutdown",
170 .data
= domain_shutdown_data
,
173 .service_id
= "domain-panic",
174 .data
= domain_panic_data
,
176 #ifdef CONFIG_HOTPLUG_CPU
178 .service_id
= "dr-cpu",
187 .service_id
= "var-config",
191 .service_id
= "var-config-backup",
196 static DEFINE_SPINLOCK(ds_lock
);
199 struct ldc_channel
*lp
;
201 #define DS_HS_START 0x01
202 #define DS_HS_DONE 0x02
209 struct ds_cap_state
*ds_states
;
212 struct ds_info
*next
;
215 static struct ds_info
*ds_info_list
;
217 static struct ds_cap_state
*find_cap(struct ds_info
*dp
, u64 handle
)
219 unsigned int index
= handle
>> 32;
221 if (index
>= dp
->num_ds_states
)
223 return &dp
->ds_states
[index
];
226 static struct ds_cap_state
*find_cap_by_string(struct ds_info
*dp
,
231 for (i
= 0; i
< dp
->num_ds_states
; i
++) {
232 if (strcmp(dp
->ds_states
[i
].service_id
, name
))
235 return &dp
->ds_states
[i
];
240 static int __ds_send(struct ldc_channel
*lp
, void *data
, int len
)
242 int err
, limit
= 1000;
245 while (limit
-- > 0) {
246 err
= ldc_write(lp
, data
, len
);
247 if (!err
|| (err
!= -EAGAIN
))
255 static int ds_send(struct ldc_channel
*lp
, void *data
, int len
)
260 spin_lock_irqsave(&ds_lock
, flags
);
261 err
= __ds_send(lp
, data
, len
);
262 spin_unlock_irqrestore(&ds_lock
, flags
);
267 struct ds_md_update_req
{
271 struct ds_md_update_res
{
276 static void md_update_data(struct ds_info
*dp
,
277 struct ds_cap_state
*cp
,
280 struct ldc_channel
*lp
= dp
->lp
;
281 struct ds_data
*dpkt
= buf
;
282 struct ds_md_update_req
*rp
;
285 struct ds_md_update_res res
;
288 rp
= (struct ds_md_update_req
*) (dpkt
+ 1);
290 printk(KERN_INFO
"ds-%lu: Machine description update.\n", dp
->id
);
294 memset(&pkt
, 0, sizeof(pkt
));
295 pkt
.data
.tag
.type
= DS_DATA
;
296 pkt
.data
.tag
.len
= sizeof(pkt
) - sizeof(struct ds_msg_tag
);
297 pkt
.data
.handle
= cp
->handle
;
298 pkt
.res
.req_num
= rp
->req_num
;
299 pkt
.res
.result
= DS_OK
;
301 ds_send(lp
, &pkt
, sizeof(pkt
));
304 struct ds_shutdown_req
{
309 struct ds_shutdown_res
{
315 static void domain_shutdown_data(struct ds_info
*dp
,
316 struct ds_cap_state
*cp
,
319 struct ldc_channel
*lp
= dp
->lp
;
320 struct ds_data
*dpkt
= buf
;
321 struct ds_shutdown_req
*rp
;
324 struct ds_shutdown_res res
;
327 rp
= (struct ds_shutdown_req
*) (dpkt
+ 1);
329 printk(KERN_ALERT
"ds-%lu: Shutdown request from "
330 "LDOM manager received.\n", dp
->id
);
332 memset(&pkt
, 0, sizeof(pkt
));
333 pkt
.data
.tag
.type
= DS_DATA
;
334 pkt
.data
.tag
.len
= sizeof(pkt
) - sizeof(struct ds_msg_tag
);
335 pkt
.data
.handle
= cp
->handle
;
336 pkt
.res
.req_num
= rp
->req_num
;
337 pkt
.res
.result
= DS_OK
;
338 pkt
.res
.reason
[0] = 0;
340 ds_send(lp
, &pkt
, sizeof(pkt
));
342 orderly_poweroff(true);
345 struct ds_panic_req
{
349 struct ds_panic_res
{
355 static void domain_panic_data(struct ds_info
*dp
,
356 struct ds_cap_state
*cp
,
359 struct ldc_channel
*lp
= dp
->lp
;
360 struct ds_data
*dpkt
= buf
;
361 struct ds_panic_req
*rp
;
364 struct ds_panic_res res
;
367 rp
= (struct ds_panic_req
*) (dpkt
+ 1);
369 printk(KERN_ALERT
"ds-%lu: Panic request from "
370 "LDOM manager received.\n", dp
->id
);
372 memset(&pkt
, 0, sizeof(pkt
));
373 pkt
.data
.tag
.type
= DS_DATA
;
374 pkt
.data
.tag
.len
= sizeof(pkt
) - sizeof(struct ds_msg_tag
);
375 pkt
.data
.handle
= cp
->handle
;
376 pkt
.res
.req_num
= rp
->req_num
;
377 pkt
.res
.result
= DS_OK
;
378 pkt
.res
.reason
[0] = 0;
380 ds_send(lp
, &pkt
, sizeof(pkt
));
382 panic("PANIC requested by LDOM manager.");
385 #ifdef CONFIG_HOTPLUG_CPU
389 #define DR_CPU_CONFIGURE 0x43
390 #define DR_CPU_UNCONFIGURE 0x55
391 #define DR_CPU_FORCE_UNCONFIGURE 0x46
392 #define DR_CPU_STATUS 0x53
395 #define DR_CPU_OK 0x6f
396 #define DR_CPU_ERROR 0x65
401 struct dr_cpu_resp_entry
{
404 #define DR_CPU_RES_OK 0x00
405 #define DR_CPU_RES_FAILURE 0x01
406 #define DR_CPU_RES_BLOCKED 0x02
407 #define DR_CPU_RES_CPU_NOT_RESPONDING 0x03
408 #define DR_CPU_RES_NOT_IN_MD 0x04
411 #define DR_CPU_STAT_NOT_PRESENT 0x00
412 #define DR_CPU_STAT_UNCONFIGURED 0x01
413 #define DR_CPU_STAT_CONFIGURED 0x02
418 static void __dr_cpu_send_error(struct ds_info
*dp
,
419 struct ds_cap_state
*cp
,
420 struct ds_data
*data
)
422 struct dr_cpu_tag
*tag
= (struct dr_cpu_tag
*) (data
+ 1);
425 struct dr_cpu_tag tag
;
429 memset(&pkt
, 0, sizeof(pkt
));
430 pkt
.data
.tag
.type
= DS_DATA
;
431 pkt
.data
.handle
= cp
->handle
;
432 pkt
.tag
.req_num
= tag
->req_num
;
433 pkt
.tag
.type
= DR_CPU_ERROR
;
434 pkt
.tag
.num_records
= 0;
436 msg_len
= (sizeof(struct ds_data
) +
437 sizeof(struct dr_cpu_tag
));
439 pkt
.data
.tag
.len
= msg_len
- sizeof(struct ds_msg_tag
);
441 __ds_send(dp
->lp
, &pkt
, msg_len
);
444 static void dr_cpu_send_error(struct ds_info
*dp
,
445 struct ds_cap_state
*cp
,
446 struct ds_data
*data
)
450 spin_lock_irqsave(&ds_lock
, flags
);
451 __dr_cpu_send_error(dp
, cp
, data
);
452 spin_unlock_irqrestore(&ds_lock
, flags
);
455 #define CPU_SENTINEL 0xffffffff
457 static void purge_dups(u32
*list
, u32 num_ents
)
461 for (i
= 0; i
< num_ents
; i
++) {
465 if (cpu
== CPU_SENTINEL
)
468 for (j
= i
+ 1; j
< num_ents
; j
++) {
470 list
[j
] = CPU_SENTINEL
;
475 static int dr_cpu_size_response(int ncpus
)
477 return (sizeof(struct ds_data
) +
478 sizeof(struct dr_cpu_tag
) +
479 (sizeof(struct dr_cpu_resp_entry
) * ncpus
));
482 static void dr_cpu_init_response(struct ds_data
*resp
, u64 req_num
,
483 u64 handle
, int resp_len
, int ncpus
,
484 cpumask_t
*mask
, u32 default_stat
)
486 struct dr_cpu_resp_entry
*ent
;
487 struct dr_cpu_tag
*tag
;
490 tag
= (struct dr_cpu_tag
*) (resp
+ 1);
491 ent
= (struct dr_cpu_resp_entry
*) (tag
+ 1);
493 resp
->tag
.type
= DS_DATA
;
494 resp
->tag
.len
= resp_len
- sizeof(struct ds_msg_tag
);
495 resp
->handle
= handle
;
496 tag
->req_num
= req_num
;
497 tag
->type
= DR_CPU_OK
;
498 tag
->num_records
= ncpus
;
501 for_each_cpu_mask(cpu
, *mask
) {
503 ent
[i
].result
= DR_CPU_RES_OK
;
504 ent
[i
].stat
= default_stat
;
510 static void dr_cpu_mark(struct ds_data
*resp
, int cpu
, int ncpus
,
513 struct dr_cpu_resp_entry
*ent
;
514 struct dr_cpu_tag
*tag
;
517 tag
= (struct dr_cpu_tag
*) (resp
+ 1);
518 ent
= (struct dr_cpu_resp_entry
*) (tag
+ 1);
520 for (i
= 0; i
< ncpus
; i
++) {
521 if (ent
[i
].cpu
!= cpu
)
529 static int dr_cpu_configure(struct ds_info
*dp
,
530 struct ds_cap_state
*cp
,
534 struct ds_data
*resp
;
535 int resp_len
, ncpus
, cpu
;
538 ncpus
= cpus_weight(*mask
);
539 resp_len
= dr_cpu_size_response(ncpus
);
540 resp
= kzalloc(resp_len
, GFP_KERNEL
);
544 dr_cpu_init_response(resp
, req_num
, cp
->handle
,
545 resp_len
, ncpus
, mask
,
546 DR_CPU_STAT_CONFIGURED
);
548 mdesc_fill_in_cpu_data(*mask
);
550 for_each_cpu_mask(cpu
, *mask
) {
553 printk(KERN_INFO
"ds-%lu: Starting cpu %d...\n",
557 __u32 res
= DR_CPU_RES_FAILURE
;
558 __u32 stat
= DR_CPU_STAT_UNCONFIGURED
;
560 if (!cpu_present(cpu
)) {
561 /* CPU not present in MD */
562 res
= DR_CPU_RES_NOT_IN_MD
;
563 stat
= DR_CPU_STAT_NOT_PRESENT
;
564 } else if (err
== -ENODEV
) {
565 /* CPU did not call in successfully */
566 res
= DR_CPU_RES_CPU_NOT_RESPONDING
;
569 printk(KERN_INFO
"ds-%lu: CPU startup failed err=%d\n",
571 dr_cpu_mark(resp
, cpu
, ncpus
, res
, stat
);
575 spin_lock_irqsave(&ds_lock
, flags
);
576 __ds_send(dp
->lp
, resp
, resp_len
);
577 spin_unlock_irqrestore(&ds_lock
, flags
);
581 /* Redistribute IRQs, taking into account the new cpus. */
587 static int dr_cpu_unconfigure(struct ds_info
*dp
,
588 struct ds_cap_state
*cp
,
592 struct ds_data
*resp
;
593 int resp_len
, ncpus
, cpu
;
596 ncpus
= cpus_weight(*mask
);
597 resp_len
= dr_cpu_size_response(ncpus
);
598 resp
= kzalloc(resp_len
, GFP_KERNEL
);
602 dr_cpu_init_response(resp
, req_num
, cp
->handle
,
603 resp_len
, ncpus
, mask
,
604 DR_CPU_STAT_UNCONFIGURED
);
606 for_each_cpu_mask(cpu
, *mask
) {
609 printk(KERN_INFO
"ds-%lu: Shutting down cpu %d...\n",
613 dr_cpu_mark(resp
, cpu
, ncpus
,
615 DR_CPU_STAT_CONFIGURED
);
618 spin_lock_irqsave(&ds_lock
, flags
);
619 __ds_send(dp
->lp
, resp
, resp_len
);
620 spin_unlock_irqrestore(&ds_lock
, flags
);
627 static void dr_cpu_data(struct ds_info
*dp
,
628 struct ds_cap_state
*cp
,
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
;
640 case DR_CPU_CONFIGURE
:
641 case DR_CPU_UNCONFIGURE
:
642 case DR_CPU_FORCE_UNCONFIGURE
:
646 dr_cpu_send_error(dp
, cp
, data
);
650 purge_dups(cpu_list
, tag
->num_records
);
653 for (i
= 0; i
< tag
->num_records
; i
++) {
654 if (cpu_list
[i
] == CPU_SENTINEL
)
657 if (cpu_list
[i
] < NR_CPUS
)
658 cpu_set(cpu_list
[i
], mask
);
661 if (tag
->type
== DR_CPU_CONFIGURE
)
662 err
= dr_cpu_configure(dp
, cp
, req_num
, &mask
);
664 err
= dr_cpu_unconfigure(dp
, cp
, req_num
, &mask
);
667 dr_cpu_send_error(dp
, cp
, data
);
669 #endif /* CONFIG_HOTPLUG_CPU */
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
,
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-%lu: PRI REQ [%lx:%lx], len=%d\n",
689 dp
->id
, rp
->req_num
, rp
->type
, len
);
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
;
711 struct ds_var_hdr hdr
;
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
,
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
)
737 ds_var_response
= rp
->result
;
742 void ldom_set_var(const char *var
, const char *value
)
744 struct ds_cap_state
*cp
;
748 spin_lock_irqsave(&ds_lock
, flags
);
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
) {
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
) {
770 spin_unlock_irqrestore(&ds_lock
, flags
);
776 struct ds_var_set_msg msg
;
783 memset(&pkt
, 0, sizeof(pkt
));
784 pkt
.header
.data
.tag
.type
= DS_DATA
;
785 pkt
.header
.data
.handle
= cp
->handle
;
786 pkt
.header
.msg
.hdr
.type
= DS_VAR_SET_REQ
;
787 base
= p
= &pkt
.header
.msg
.name_and_value
[0];
789 p
+= strlen(var
) + 1;
791 p
+= strlen(value
) + 1;
793 msg_len
= (sizeof(struct ds_data
) +
794 sizeof(struct ds_var_set_msg
) +
796 msg_len
= (msg_len
+ 3) & ~3;
797 pkt
.header
.data
.tag
.len
= msg_len
- sizeof(struct ds_msg_tag
);
799 mutex_lock(&ds_var_mutex
);
801 spin_lock_irqsave(&ds_lock
, flags
);
803 ds_var_response
= -1;
805 __ds_send(dp
->lp
, &pkt
, msg_len
);
806 spin_unlock_irqrestore(&ds_lock
, flags
);
809 while (ds_var_doorbell
== 0) {
816 mutex_unlock(&ds_var_mutex
);
818 if (ds_var_doorbell
== 0 ||
819 ds_var_response
!= DS_VAR_SUCCESS
)
820 printk(KERN_ERR
"ds-%lu: var-config [%s:%s] "
821 "failed, response(%d).\n",
825 printk(KERN_ERR PFX
"var-config not registered so "
826 "could not set (%s) variable to (%s).\n",
831 void ldom_reboot(const char *boot_command
)
833 /* Don't bother with any of this if the boot_command
836 if (boot_command
&& strlen(boot_command
)) {
837 char full_boot_str
[256];
839 strcpy(full_boot_str
, "boot ");
840 strcpy(full_boot_str
+ strlen("boot "), boot_command
);
842 ldom_set_var("reboot-command", full_boot_str
);
847 void ldom_power_off(void)
852 static void ds_conn_reset(struct ds_info
*dp
)
854 printk(KERN_ERR
"ds-%lu: ds_conn_reset() from %p\n",
855 dp
->id
, __builtin_return_address(0));
858 static int register_services(struct ds_info
*dp
)
860 struct ldc_channel
*lp
= dp
->lp
;
863 for (i
= 0; i
< dp
->num_ds_states
; i
++) {
865 struct ds_reg_req req
;
868 struct ds_cap_state
*cp
= &dp
->ds_states
[i
];
872 if (cp
->state
== CAP_STATE_REGISTERED
)
875 new_count
= sched_clock() & 0xffffffff;
876 cp
->handle
= ((u64
) i
<< 32) | new_count
;
878 msg_len
= (sizeof(struct ds_reg_req
) +
879 strlen(cp
->service_id
));
881 memset(&pbuf
, 0, sizeof(pbuf
));
882 pbuf
.req
.tag
.type
= DS_REG_REQ
;
883 pbuf
.req
.tag
.len
= (msg_len
- sizeof(struct ds_msg_tag
));
884 pbuf
.req
.handle
= cp
->handle
;
887 strcpy(pbuf
.req
.svc_id
, cp
->service_id
);
889 err
= __ds_send(lp
, &pbuf
, msg_len
);
891 cp
->state
= CAP_STATE_REG_SENT
;
896 static int ds_handshake(struct ds_info
*dp
, struct ds_msg_tag
*pkt
)
899 if (dp
->hs_state
== DS_HS_START
) {
900 if (pkt
->type
!= DS_INIT_ACK
)
903 dp
->hs_state
= DS_HS_DONE
;
905 return register_services(dp
);
908 if (dp
->hs_state
!= DS_HS_DONE
)
911 if (pkt
->type
== DS_REG_ACK
) {
912 struct ds_reg_ack
*ap
= (struct ds_reg_ack
*) pkt
;
913 struct ds_cap_state
*cp
= find_cap(dp
, ap
->handle
);
916 printk(KERN_ERR
"ds-%lu: REG ACK for unknown "
917 "handle %lx\n", dp
->id
, ap
->handle
);
920 printk(KERN_INFO
"ds-%lu: Registered %s service.\n",
921 dp
->id
, cp
->service_id
);
922 cp
->state
= CAP_STATE_REGISTERED
;
923 } else if (pkt
->type
== DS_REG_NACK
) {
924 struct ds_reg_nack
*np
= (struct ds_reg_nack
*) pkt
;
925 struct ds_cap_state
*cp
= find_cap(dp
, np
->handle
);
928 printk(KERN_ERR
"ds-%lu: REG NACK for "
929 "unknown handle %lx\n",
933 cp
->state
= CAP_STATE_UNKNOWN
;
943 static void __send_ds_nack(struct ds_info
*dp
, u64 handle
)
945 struct ds_data_nack nack
= {
948 .len
= (sizeof(struct ds_data_nack
) -
949 sizeof(struct ds_msg_tag
)),
952 .result
= DS_INV_HDL
,
955 __ds_send(dp
->lp
, &nack
, sizeof(nack
));
958 static LIST_HEAD(ds_work_list
);
959 static DECLARE_WAIT_QUEUE_HEAD(ds_wait
);
961 struct ds_queue_entry
{
962 struct list_head list
;
969 static void process_ds_work(void)
971 struct ds_queue_entry
*qp
, *tmp
;
975 spin_lock_irqsave(&ds_lock
, flags
);
976 list_splice(&ds_work_list
, &todo
);
977 INIT_LIST_HEAD(&ds_work_list
);
978 spin_unlock_irqrestore(&ds_lock
, flags
);
980 list_for_each_entry_safe(qp
, tmp
, &todo
, list
) {
981 struct ds_data
*dpkt
= (struct ds_data
*) qp
->req
;
982 struct ds_info
*dp
= qp
->dp
;
983 struct ds_cap_state
*cp
= find_cap(dp
, dpkt
->handle
);
984 int req_len
= qp
->req_len
;
987 printk(KERN_ERR
"ds-%lu: Data for unknown "
989 dp
->id
, dpkt
->handle
);
991 spin_lock_irqsave(&ds_lock
, flags
);
992 __send_ds_nack(dp
, dpkt
->handle
);
993 spin_unlock_irqrestore(&ds_lock
, flags
);
995 cp
->data(dp
, cp
, dpkt
, req_len
);
1003 static int ds_thread(void *__unused
)
1008 prepare_to_wait(&ds_wait
, &wait
, TASK_INTERRUPTIBLE
);
1009 if (list_empty(&ds_work_list
))
1011 finish_wait(&ds_wait
, &wait
);
1013 if (kthread_should_stop())
1022 static int ds_data(struct ds_info
*dp
, struct ds_msg_tag
*pkt
, int len
)
1024 struct ds_data
*dpkt
= (struct ds_data
*) pkt
;
1025 struct ds_queue_entry
*qp
;
1027 qp
= kmalloc(sizeof(struct ds_queue_entry
) + len
, GFP_ATOMIC
);
1029 __send_ds_nack(dp
, dpkt
->handle
);
1032 memcpy(&qp
->req
, pkt
, len
);
1033 list_add_tail(&qp
->list
, &ds_work_list
);
1039 static void ds_up(struct ds_info
*dp
)
1041 struct ldc_channel
*lp
= dp
->lp
;
1042 struct ds_ver_req req
;
1045 req
.tag
.type
= DS_INIT_REQ
;
1046 req
.tag
.len
= sizeof(req
) - sizeof(struct ds_msg_tag
);
1050 err
= __ds_send(lp
, &req
, sizeof(req
));
1052 dp
->hs_state
= DS_HS_START
;
1055 static void ds_reset(struct ds_info
*dp
)
1061 for (i
= 0; i
< dp
->num_ds_states
; i
++) {
1062 struct ds_cap_state
*cp
= &dp
->ds_states
[i
];
1064 cp
->state
= CAP_STATE_UNKNOWN
;
1068 static void ds_event(void *arg
, int event
)
1070 struct ds_info
*dp
= arg
;
1071 struct ldc_channel
*lp
= dp
->lp
;
1072 unsigned long flags
;
1075 spin_lock_irqsave(&ds_lock
, flags
);
1077 if (event
== LDC_EVENT_UP
) {
1079 spin_unlock_irqrestore(&ds_lock
, flags
);
1083 if (event
== LDC_EVENT_RESET
) {
1085 spin_unlock_irqrestore(&ds_lock
, flags
);
1089 if (event
!= LDC_EVENT_DATA_READY
) {
1090 printk(KERN_WARNING
"ds-%lu: Unexpected LDC event %d\n",
1092 spin_unlock_irqrestore(&ds_lock
, flags
);
1098 struct ds_msg_tag
*tag
;
1100 err
= ldc_read(lp
, dp
->rcv_buf
, sizeof(*tag
));
1102 if (unlikely(err
< 0)) {
1103 if (err
== -ECONNRESET
)
1111 err
= ldc_read(lp
, tag
+ 1, tag
->len
);
1113 if (unlikely(err
< 0)) {
1114 if (err
== -ECONNRESET
)
1121 if (tag
->type
< DS_DATA
)
1122 err
= ds_handshake(dp
, dp
->rcv_buf
);
1124 err
= ds_data(dp
, dp
->rcv_buf
,
1125 sizeof(*tag
) + err
);
1126 if (err
== -ECONNRESET
)
1130 spin_unlock_irqrestore(&ds_lock
, flags
);
1133 static int __devinit
ds_probe(struct vio_dev
*vdev
,
1134 const struct vio_device_id
*id
)
1136 static int ds_version_printed
;
1137 struct ldc_channel_config ds_cfg
= {
1140 .mode
= LDC_MODE_STREAM
,
1142 struct mdesc_handle
*hp
;
1143 struct ldc_channel
*lp
;
1148 if (ds_version_printed
++ == 0)
1149 printk(KERN_INFO
"%s", version
);
1151 dp
= kzalloc(sizeof(*dp
), GFP_KERNEL
);
1157 val
= mdesc_get_property(hp
, vdev
->mp
, "id", NULL
);
1162 dp
->rcv_buf
= kzalloc(4096, GFP_KERNEL
);
1166 dp
->rcv_buf_len
= 4096;
1168 dp
->ds_states
= kzalloc(sizeof(ds_states_template
),
1171 goto out_free_rcv_buf
;
1173 memcpy(dp
->ds_states
, ds_states_template
,
1174 sizeof(ds_states_template
));
1175 dp
->num_ds_states
= ARRAY_SIZE(ds_states_template
);
1177 for (i
= 0; i
< dp
->num_ds_states
; i
++)
1178 dp
->ds_states
[i
].handle
= ((u64
)i
<< 32);
1180 ds_cfg
.tx_irq
= vdev
->tx_irq
;
1181 ds_cfg
.rx_irq
= vdev
->rx_irq
;
1183 lp
= ldc_alloc(vdev
->channel_id
, &ds_cfg
, dp
);
1186 goto out_free_ds_states
;
1190 err
= ldc_bind(lp
, "DS");
1194 spin_lock_irq(&ds_lock
);
1195 dp
->next
= ds_info_list
;
1197 spin_unlock_irq(&ds_lock
);
1205 kfree(dp
->ds_states
);
1217 static int ds_remove(struct vio_dev
*vdev
)
1222 static struct vio_device_id ds_match
[] = {
1224 .type
= "domain-services-port",
1229 static struct vio_driver ds_driver
= {
1230 .id_table
= ds_match
,
1232 .remove
= ds_remove
,
1235 .owner
= THIS_MODULE
,
1239 static int __init
ds_init(void)
1241 kthread_run(ds_thread
, NULL
, "kldomd");
1243 return vio_register_driver(&ds_driver
);
1246 subsys_initcall(ds_init
);