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/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>
20 #include <asm/mdesc.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
);
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
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
66 struct ds_msg_tag tag
;
67 struct ds_version ver
;
71 struct ds_msg_tag tag
;
76 struct ds_msg_tag tag
;
81 struct ds_msg_tag tag
;
89 struct ds_msg_tag tag
;
95 struct ds_msg_tag tag
;
100 struct ds_unreg_req
{
101 struct ds_msg_tag tag
;
105 struct ds_unreg_ack
{
106 struct ds_msg_tag tag
;
110 struct ds_unreg_nack
{
111 struct ds_msg_tag tag
;
116 struct ds_msg_tag tag
;
120 struct ds_data_nack
{
121 struct ds_msg_tag tag
;
127 struct ds_cap_state
{
130 void (*data
)(struct ds_info
*dp
,
131 struct ds_cap_state
*cp
,
134 const char *service_id
;
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
,
144 static void domain_shutdown_data(struct ds_info
*dp
,
145 struct ds_cap_state
*cp
,
147 static void domain_panic_data(struct ds_info
*dp
,
148 struct ds_cap_state
*cp
,
150 #ifdef CONFIG_HOTPLUG_CPU
151 static void dr_cpu_data(struct ds_info
*dp
,
152 struct ds_cap_state
*cp
,
155 static void ds_pri_data(struct ds_info
*dp
,
156 struct ds_cap_state
*cp
,
158 static void ds_var_data(struct ds_info
*dp
,
159 struct ds_cap_state
*cp
,
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",
186 .service_id
= "var-config",
190 .service_id
= "var-config-backup",
195 static DEFINE_SPINLOCK(ds_lock
);
198 struct ldc_channel
*lp
;
200 #define DS_HS_START 0x01
201 #define DS_HS_DONE 0x02
208 struct ds_cap_state
*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
)
222 return &dp
->ds_states
[index
];
225 static struct ds_cap_state
*find_cap_by_string(struct ds_info
*dp
,
230 for (i
= 0; i
< dp
->num_ds_states
; i
++) {
231 if (strcmp(dp
->ds_states
[i
].service_id
, name
))
234 return &dp
->ds_states
[i
];
239 static int __ds_send(struct ldc_channel
*lp
, void *data
, int len
)
241 int err
, limit
= 1000;
244 while (limit
-- > 0) {
245 err
= ldc_write(lp
, data
, len
);
246 if (!err
|| (err
!= -EAGAIN
))
254 static int ds_send(struct ldc_channel
*lp
, void *data
, int len
)
259 spin_lock_irqsave(&ds_lock
, flags
);
260 err
= __ds_send(lp
, data
, len
);
261 spin_unlock_irqrestore(&ds_lock
, flags
);
266 struct ds_md_update_req
{
270 struct ds_md_update_res
{
275 static void md_update_data(struct ds_info
*dp
,
276 struct ds_cap_state
*cp
,
279 struct ldc_channel
*lp
= dp
->lp
;
280 struct ds_data
*dpkt
= buf
;
281 struct ds_md_update_req
*rp
;
284 struct ds_md_update_res res
;
287 rp
= (struct ds_md_update_req
*) (dpkt
+ 1);
289 printk(KERN_INFO
"ds-%lu: Machine description update.\n", dp
->id
);
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
{
308 struct ds_shutdown_res
{
314 static void domain_shutdown_data(struct ds_info
*dp
,
315 struct ds_cap_state
*cp
,
318 struct ldc_channel
*lp
= dp
->lp
;
319 struct ds_data
*dpkt
= buf
;
320 struct ds_shutdown_req
*rp
;
323 struct ds_shutdown_res res
;
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
{
348 struct ds_panic_res
{
354 static void domain_panic_data(struct ds_info
*dp
,
355 struct ds_cap_state
*cp
,
358 struct ldc_channel
*lp
= dp
->lp
;
359 struct ds_data
*dpkt
= buf
;
360 struct ds_panic_req
*rp
;
363 struct ds_panic_res res
;
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
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
394 #define DR_CPU_OK 0x6f
395 #define DR_CPU_ERROR 0x65
400 struct dr_cpu_resp_entry
{
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
410 #define DR_CPU_STAT_NOT_PRESENT 0x00
411 #define DR_CPU_STAT_UNCONFIGURED 0x01
412 #define DR_CPU_STAT_CONFIGURED 0x02
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);
424 struct dr_cpu_tag tag
;
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
)
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
)
460 for (i
= 0; i
< num_ents
; i
++) {
464 if (cpu
== CPU_SENTINEL
)
467 for (j
= i
+ 1; j
< num_ents
; j
++) {
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
;
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
;
500 for_each_cpu_mask(cpu
, *mask
) {
502 ent
[i
].result
= DR_CPU_RES_OK
;
503 ent
[i
].stat
= default_stat
;
509 static void dr_cpu_mark(struct ds_data
*resp
, int cpu
, int ncpus
,
512 struct dr_cpu_resp_entry
*ent
;
513 struct dr_cpu_tag
*tag
;
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
)
528 static int __cpuinit
dr_cpu_configure(struct ds_info
*dp
,
529 struct ds_cap_state
*cp
,
533 struct ds_data
*resp
;
534 int resp_len
, ncpus
, cpu
;
537 ncpus
= cpus_weight(*mask
);
538 resp_len
= dr_cpu_size_response(ncpus
);
539 resp
= kzalloc(resp_len
, GFP_KERNEL
);
543 dr_cpu_init_response(resp
, req_num
, cp
->handle
,
544 resp_len
, ncpus
, mask
,
545 DR_CPU_STAT_CONFIGURED
);
547 mdesc_fill_in_cpu_data(*mask
);
549 for_each_cpu_mask(cpu
, *mask
) {
552 printk(KERN_INFO
"ds-%lu: Starting cpu %d...\n",
556 __u32 res
= DR_CPU_RES_FAILURE
;
557 __u32 stat
= DR_CPU_STAT_UNCONFIGURED
;
559 if (!cpu_present(cpu
)) {
560 /* CPU not present in MD */
561 res
= DR_CPU_RES_NOT_IN_MD
;
562 stat
= DR_CPU_STAT_NOT_PRESENT
;
563 } else if (err
== -ENODEV
) {
564 /* CPU did not call in successfully */
565 res
= DR_CPU_RES_CPU_NOT_RESPONDING
;
568 printk(KERN_INFO
"ds-%lu: CPU startup failed err=%d\n",
570 dr_cpu_mark(resp
, cpu
, ncpus
, res
, stat
);
574 spin_lock_irqsave(&ds_lock
, flags
);
575 __ds_send(dp
->lp
, resp
, resp_len
);
576 spin_unlock_irqrestore(&ds_lock
, flags
);
580 /* Redistribute IRQs, taking into account the new cpus. */
586 static int dr_cpu_unconfigure(struct ds_info
*dp
,
587 struct ds_cap_state
*cp
,
591 struct ds_data
*resp
;
592 int resp_len
, ncpus
, cpu
;
595 ncpus
= cpus_weight(*mask
);
596 resp_len
= dr_cpu_size_response(ncpus
);
597 resp
= kzalloc(resp_len
, GFP_KERNEL
);
601 dr_cpu_init_response(resp
, req_num
, cp
->handle
,
602 resp_len
, ncpus
, mask
,
603 DR_CPU_STAT_UNCONFIGURED
);
605 for_each_cpu_mask(cpu
, *mask
) {
608 printk(KERN_INFO
"ds-%lu: Shutting down cpu %d...\n",
612 dr_cpu_mark(resp
, cpu
, ncpus
,
614 DR_CPU_STAT_CONFIGURED
);
617 spin_lock_irqsave(&ds_lock
, flags
);
618 __ds_send(dp
->lp
, resp
, resp_len
);
619 spin_unlock_irqrestore(&ds_lock
, flags
);
626 static void __cpuinit
dr_cpu_data(struct ds_info
*dp
,
627 struct ds_cap_state
*cp
,
630 struct ds_data
*data
= buf
;
631 struct dr_cpu_tag
*tag
= (struct dr_cpu_tag
*) (data
+ 1);
632 u32
*cpu_list
= (u32
*) (tag
+ 1);
633 u64 req_num
= tag
->req_num
;
639 case DR_CPU_CONFIGURE
:
640 case DR_CPU_UNCONFIGURE
:
641 case DR_CPU_FORCE_UNCONFIGURE
:
645 dr_cpu_send_error(dp
, cp
, data
);
649 purge_dups(cpu_list
, tag
->num_records
);
652 for (i
= 0; i
< tag
->num_records
; i
++) {
653 if (cpu_list
[i
] == CPU_SENTINEL
)
656 if (cpu_list
[i
] < NR_CPUS
)
657 cpu_set(cpu_list
[i
], mask
);
660 if (tag
->type
== DR_CPU_CONFIGURE
)
661 err
= dr_cpu_configure(dp
, cp
, req_num
, &mask
);
663 err
= dr_cpu_unconfigure(dp
, cp
, req_num
, &mask
);
666 dr_cpu_send_error(dp
, cp
, data
);
668 #endif /* CONFIG_HOTPLUG_CPU */
673 #define DS_PRI_REQUEST 0x00
674 #define DS_PRI_DATA 0x01
675 #define DS_PRI_UPDATE 0x02
678 static void ds_pri_data(struct ds_info
*dp
,
679 struct ds_cap_state
*cp
,
682 struct ds_data
*dpkt
= buf
;
683 struct ds_pri_msg
*rp
;
685 rp
= (struct ds_pri_msg
*) (dpkt
+ 1);
687 printk(KERN_INFO
"ds-%lu: PRI REQ [%lx:%lx], len=%d\n",
688 dp
->id
, rp
->req_num
, rp
->type
, len
);
693 #define DS_VAR_SET_REQ 0x00
694 #define DS_VAR_DELETE_REQ 0x01
695 #define DS_VAR_SET_RESP 0x02
696 #define DS_VAR_DELETE_RESP 0x03
699 struct ds_var_set_msg
{
700 struct ds_var_hdr hdr
;
701 char name_and_value
[0];
704 struct ds_var_delete_msg
{
705 struct ds_var_hdr hdr
;
710 struct ds_var_hdr hdr
;
712 #define DS_VAR_SUCCESS 0x00
713 #define DS_VAR_NO_SPACE 0x01
714 #define DS_VAR_INVALID_VAR 0x02
715 #define DS_VAR_INVALID_VAL 0x03
716 #define DS_VAR_NOT_PRESENT 0x04
719 static DEFINE_MUTEX(ds_var_mutex
);
720 static int ds_var_doorbell
;
721 static int ds_var_response
;
723 static void ds_var_data(struct ds_info
*dp
,
724 struct ds_cap_state
*cp
,
727 struct ds_data
*dpkt
= buf
;
728 struct ds_var_resp
*rp
;
730 rp
= (struct ds_var_resp
*) (dpkt
+ 1);
732 if (rp
->hdr
.type
!= DS_VAR_SET_RESP
&&
733 rp
->hdr
.type
!= DS_VAR_DELETE_RESP
)
736 ds_var_response
= rp
->result
;
741 void ldom_set_var(const char *var
, const char *value
)
743 struct ds_cap_state
*cp
;
747 spin_lock_irqsave(&ds_lock
, flags
);
749 for (dp
= ds_info_list
; dp
; dp
= dp
->next
) {
750 struct ds_cap_state
*tmp
;
752 tmp
= find_cap_by_string(dp
, "var-config");
753 if (tmp
&& tmp
->state
== CAP_STATE_REGISTERED
) {
759 for (dp
= ds_info_list
; dp
; dp
= dp
->next
) {
760 struct ds_cap_state
*tmp
;
762 tmp
= find_cap_by_string(dp
, "var-config-backup");
763 if (tmp
&& tmp
->state
== CAP_STATE_REGISTERED
) {
769 spin_unlock_irqrestore(&ds_lock
, flags
);
775 struct ds_var_set_msg msg
;
782 memset(&pkt
, 0, sizeof(pkt
));
783 pkt
.header
.data
.tag
.type
= DS_DATA
;
784 pkt
.header
.data
.handle
= cp
->handle
;
785 pkt
.header
.msg
.hdr
.type
= DS_VAR_SET_REQ
;
786 base
= p
= &pkt
.header
.msg
.name_and_value
[0];
788 p
+= strlen(var
) + 1;
790 p
+= strlen(value
) + 1;
792 msg_len
= (sizeof(struct ds_data
) +
793 sizeof(struct ds_var_set_msg
) +
795 msg_len
= (msg_len
+ 3) & ~3;
796 pkt
.header
.data
.tag
.len
= msg_len
- sizeof(struct ds_msg_tag
);
798 mutex_lock(&ds_var_mutex
);
800 spin_lock_irqsave(&ds_lock
, flags
);
802 ds_var_response
= -1;
804 __ds_send(dp
->lp
, &pkt
, msg_len
);
805 spin_unlock_irqrestore(&ds_lock
, flags
);
808 while (ds_var_doorbell
== 0) {
815 mutex_unlock(&ds_var_mutex
);
817 if (ds_var_doorbell
== 0 ||
818 ds_var_response
!= DS_VAR_SUCCESS
)
819 printk(KERN_ERR
"ds-%lu: var-config [%s:%s] "
820 "failed, response(%d).\n",
824 printk(KERN_ERR PFX
"var-config not registered so "
825 "could not set (%s) variable to (%s).\n",
830 void ldom_reboot(const char *boot_command
)
832 /* Don't bother with any of this if the boot_command
835 if (boot_command
&& strlen(boot_command
)) {
836 char full_boot_str
[256];
838 strcpy(full_boot_str
, "boot ");
839 strcpy(full_boot_str
+ strlen("boot "), boot_command
);
841 ldom_set_var("reboot-command", full_boot_str
);
846 void ldom_power_off(void)
851 static void ds_conn_reset(struct ds_info
*dp
)
853 printk(KERN_ERR
"ds-%lu: ds_conn_reset() from %p\n",
854 dp
->id
, __builtin_return_address(0));
857 static int register_services(struct ds_info
*dp
)
859 struct ldc_channel
*lp
= dp
->lp
;
862 for (i
= 0; i
< dp
->num_ds_states
; i
++) {
864 struct ds_reg_req req
;
867 struct ds_cap_state
*cp
= &dp
->ds_states
[i
];
871 if (cp
->state
== CAP_STATE_REGISTERED
)
874 new_count
= sched_clock() & 0xffffffff;
875 cp
->handle
= ((u64
) i
<< 32) | new_count
;
877 msg_len
= (sizeof(struct ds_reg_req
) +
878 strlen(cp
->service_id
));
880 memset(&pbuf
, 0, sizeof(pbuf
));
881 pbuf
.req
.tag
.type
= DS_REG_REQ
;
882 pbuf
.req
.tag
.len
= (msg_len
- sizeof(struct ds_msg_tag
));
883 pbuf
.req
.handle
= cp
->handle
;
886 strcpy(pbuf
.req
.svc_id
, cp
->service_id
);
888 err
= __ds_send(lp
, &pbuf
, msg_len
);
890 cp
->state
= CAP_STATE_REG_SENT
;
895 static int ds_handshake(struct ds_info
*dp
, struct ds_msg_tag
*pkt
)
898 if (dp
->hs_state
== DS_HS_START
) {
899 if (pkt
->type
!= DS_INIT_ACK
)
902 dp
->hs_state
= DS_HS_DONE
;
904 return register_services(dp
);
907 if (dp
->hs_state
!= DS_HS_DONE
)
910 if (pkt
->type
== DS_REG_ACK
) {
911 struct ds_reg_ack
*ap
= (struct ds_reg_ack
*) pkt
;
912 struct ds_cap_state
*cp
= find_cap(dp
, ap
->handle
);
915 printk(KERN_ERR
"ds-%lu: REG ACK for unknown "
916 "handle %lx\n", dp
->id
, ap
->handle
);
919 printk(KERN_INFO
"ds-%lu: Registered %s service.\n",
920 dp
->id
, cp
->service_id
);
921 cp
->state
= CAP_STATE_REGISTERED
;
922 } else if (pkt
->type
== DS_REG_NACK
) {
923 struct ds_reg_nack
*np
= (struct ds_reg_nack
*) pkt
;
924 struct ds_cap_state
*cp
= find_cap(dp
, np
->handle
);
927 printk(KERN_ERR
"ds-%lu: REG NACK for "
928 "unknown handle %lx\n",
932 cp
->state
= CAP_STATE_UNKNOWN
;
942 static void __send_ds_nack(struct ds_info
*dp
, u64 handle
)
944 struct ds_data_nack nack
= {
947 .len
= (sizeof(struct ds_data_nack
) -
948 sizeof(struct ds_msg_tag
)),
951 .result
= DS_INV_HDL
,
954 __ds_send(dp
->lp
, &nack
, sizeof(nack
));
957 static LIST_HEAD(ds_work_list
);
958 static DECLARE_WAIT_QUEUE_HEAD(ds_wait
);
960 struct ds_queue_entry
{
961 struct list_head list
;
968 static void process_ds_work(void)
970 struct ds_queue_entry
*qp
, *tmp
;
974 spin_lock_irqsave(&ds_lock
, flags
);
975 list_splice_init(&ds_work_list
, &todo
);
976 spin_unlock_irqrestore(&ds_lock
, flags
);
978 list_for_each_entry_safe(qp
, tmp
, &todo
, list
) {
979 struct ds_data
*dpkt
= (struct ds_data
*) qp
->req
;
980 struct ds_info
*dp
= qp
->dp
;
981 struct ds_cap_state
*cp
= find_cap(dp
, dpkt
->handle
);
982 int req_len
= qp
->req_len
;
985 printk(KERN_ERR
"ds-%lu: Data for unknown "
987 dp
->id
, dpkt
->handle
);
989 spin_lock_irqsave(&ds_lock
, flags
);
990 __send_ds_nack(dp
, dpkt
->handle
);
991 spin_unlock_irqrestore(&ds_lock
, flags
);
993 cp
->data(dp
, cp
, dpkt
, req_len
);
1001 static int ds_thread(void *__unused
)
1006 prepare_to_wait(&ds_wait
, &wait
, TASK_INTERRUPTIBLE
);
1007 if (list_empty(&ds_work_list
))
1009 finish_wait(&ds_wait
, &wait
);
1011 if (kthread_should_stop())
1020 static int ds_data(struct ds_info
*dp
, struct ds_msg_tag
*pkt
, int len
)
1022 struct ds_data
*dpkt
= (struct ds_data
*) pkt
;
1023 struct ds_queue_entry
*qp
;
1025 qp
= kmalloc(sizeof(struct ds_queue_entry
) + len
, GFP_ATOMIC
);
1027 __send_ds_nack(dp
, dpkt
->handle
);
1030 memcpy(&qp
->req
, pkt
, len
);
1031 list_add_tail(&qp
->list
, &ds_work_list
);
1037 static void ds_up(struct ds_info
*dp
)
1039 struct ldc_channel
*lp
= dp
->lp
;
1040 struct ds_ver_req req
;
1043 req
.tag
.type
= DS_INIT_REQ
;
1044 req
.tag
.len
= sizeof(req
) - sizeof(struct ds_msg_tag
);
1048 err
= __ds_send(lp
, &req
, sizeof(req
));
1050 dp
->hs_state
= DS_HS_START
;
1053 static void ds_reset(struct ds_info
*dp
)
1059 for (i
= 0; i
< dp
->num_ds_states
; i
++) {
1060 struct ds_cap_state
*cp
= &dp
->ds_states
[i
];
1062 cp
->state
= CAP_STATE_UNKNOWN
;
1066 static void ds_event(void *arg
, int event
)
1068 struct ds_info
*dp
= arg
;
1069 struct ldc_channel
*lp
= dp
->lp
;
1070 unsigned long flags
;
1073 spin_lock_irqsave(&ds_lock
, flags
);
1075 if (event
== LDC_EVENT_UP
) {
1077 spin_unlock_irqrestore(&ds_lock
, flags
);
1081 if (event
== LDC_EVENT_RESET
) {
1083 spin_unlock_irqrestore(&ds_lock
, flags
);
1087 if (event
!= LDC_EVENT_DATA_READY
) {
1088 printk(KERN_WARNING
"ds-%lu: Unexpected LDC event %d\n",
1090 spin_unlock_irqrestore(&ds_lock
, flags
);
1096 struct ds_msg_tag
*tag
;
1098 err
= ldc_read(lp
, dp
->rcv_buf
, sizeof(*tag
));
1100 if (unlikely(err
< 0)) {
1101 if (err
== -ECONNRESET
)
1109 err
= ldc_read(lp
, tag
+ 1, tag
->len
);
1111 if (unlikely(err
< 0)) {
1112 if (err
== -ECONNRESET
)
1119 if (tag
->type
< DS_DATA
)
1120 err
= ds_handshake(dp
, dp
->rcv_buf
);
1122 err
= ds_data(dp
, dp
->rcv_buf
,
1123 sizeof(*tag
) + err
);
1124 if (err
== -ECONNRESET
)
1128 spin_unlock_irqrestore(&ds_lock
, flags
);
1131 static int __devinit
ds_probe(struct vio_dev
*vdev
,
1132 const struct vio_device_id
*id
)
1134 static int ds_version_printed
;
1135 struct ldc_channel_config ds_cfg
= {
1138 .mode
= LDC_MODE_STREAM
,
1140 struct mdesc_handle
*hp
;
1141 struct ldc_channel
*lp
;
1146 if (ds_version_printed
++ == 0)
1147 printk(KERN_INFO
"%s", version
);
1149 dp
= kzalloc(sizeof(*dp
), GFP_KERNEL
);
1155 val
= mdesc_get_property(hp
, vdev
->mp
, "id", NULL
);
1160 dp
->rcv_buf
= kzalloc(4096, GFP_KERNEL
);
1164 dp
->rcv_buf_len
= 4096;
1166 dp
->ds_states
= kzalloc(sizeof(ds_states_template
),
1169 goto out_free_rcv_buf
;
1171 memcpy(dp
->ds_states
, ds_states_template
,
1172 sizeof(ds_states_template
));
1173 dp
->num_ds_states
= ARRAY_SIZE(ds_states_template
);
1175 for (i
= 0; i
< dp
->num_ds_states
; i
++)
1176 dp
->ds_states
[i
].handle
= ((u64
)i
<< 32);
1178 ds_cfg
.tx_irq
= vdev
->tx_irq
;
1179 ds_cfg
.rx_irq
= vdev
->rx_irq
;
1181 lp
= ldc_alloc(vdev
->channel_id
, &ds_cfg
, dp
);
1184 goto out_free_ds_states
;
1188 err
= ldc_bind(lp
, "DS");
1192 spin_lock_irq(&ds_lock
);
1193 dp
->next
= ds_info_list
;
1195 spin_unlock_irq(&ds_lock
);
1203 kfree(dp
->ds_states
);
1215 static int ds_remove(struct vio_dev
*vdev
)
1220 static struct vio_device_id ds_match
[] = {
1222 .type
= "domain-services-port",
1227 static struct vio_driver ds_driver
= {
1228 .id_table
= ds_match
,
1230 .remove
= ds_remove
,
1233 .owner
= THIS_MODULE
,
1237 static int __init
ds_init(void)
1239 kthread_run(ds_thread
, NULL
, "kldomd");
1241 return vio_register_driver(&ds_driver
);
1244 subsys_initcall(ds_init
);