1 // SPDX-License-Identifier: GPL-2.0-only
2 /* ds.c: Domain Services driver for Logical Domains
4 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/types.h>
10 #include <linux/string.h>
11 #include <linux/slab.h>
12 #include <linux/sched.h>
13 #include <linux/sched/clock.h>
14 #include <linux/delay.h>
15 #include <linux/mutex.h>
16 #include <linux/kthread.h>
17 #include <linux/reboot.h>
18 #include <linux/cpu.h>
20 #include <asm/hypervisor.h>
23 #include <asm/mdesc.h>
29 #define DRV_MODULE_NAME "ds"
30 #define PFX DRV_MODULE_NAME ": "
31 #define DRV_MODULE_VERSION "1.0"
32 #define DRV_MODULE_RELDATE "Jul 11, 2007"
34 static char version
[] =
35 DRV_MODULE_NAME
".c:v" DRV_MODULE_VERSION
" (" DRV_MODULE_RELDATE
")\n";
36 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
37 MODULE_DESCRIPTION("Sun LDOM domain services driver");
38 MODULE_LICENSE("GPL");
39 MODULE_VERSION(DRV_MODULE_VERSION
);
43 #define DS_INIT_REQ 0x00
44 #define DS_INIT_ACK 0x01
45 #define DS_INIT_NACK 0x02
46 #define DS_REG_REQ 0x03
47 #define DS_REG_ACK 0x04
48 #define DS_REG_NACK 0x05
49 #define DS_UNREG_REQ 0x06
50 #define DS_UNREG_ACK 0x07
51 #define DS_UNREG_NACK 0x08
60 #define DS_REG_VER_NACK 0x01
61 #define DS_REG_DUP 0x02
62 #define DS_INV_HDL 0x03
63 #define DS_TYPE_UNKNOWN 0x04
71 struct ds_msg_tag tag
;
72 struct ds_version ver
;
76 struct ds_msg_tag tag
;
81 struct ds_msg_tag tag
;
86 struct ds_msg_tag tag
;
94 struct ds_msg_tag tag
;
100 struct ds_msg_tag tag
;
105 struct ds_unreg_req
{
106 struct ds_msg_tag tag
;
110 struct ds_unreg_ack
{
111 struct ds_msg_tag tag
;
115 struct ds_unreg_nack
{
116 struct ds_msg_tag tag
;
121 struct ds_msg_tag tag
;
125 struct ds_data_nack
{
126 struct ds_msg_tag tag
;
132 struct ds_cap_state
{
135 void (*data
)(struct ds_info
*dp
,
136 struct ds_cap_state
*cp
,
139 const char *service_id
;
142 #define CAP_STATE_UNKNOWN 0x00
143 #define CAP_STATE_REG_SENT 0x01
144 #define CAP_STATE_REGISTERED 0x02
147 static void md_update_data(struct ds_info
*dp
, struct ds_cap_state
*cp
,
149 static void domain_shutdown_data(struct ds_info
*dp
,
150 struct ds_cap_state
*cp
,
152 static void domain_panic_data(struct ds_info
*dp
,
153 struct ds_cap_state
*cp
,
155 #ifdef CONFIG_HOTPLUG_CPU
156 static void dr_cpu_data(struct ds_info
*dp
,
157 struct ds_cap_state
*cp
,
160 static void ds_pri_data(struct ds_info
*dp
,
161 struct ds_cap_state
*cp
,
163 static void ds_var_data(struct ds_info
*dp
,
164 struct ds_cap_state
*cp
,
167 static struct ds_cap_state ds_states_template
[] = {
169 .service_id
= "md-update",
170 .data
= md_update_data
,
173 .service_id
= "domain-shutdown",
174 .data
= domain_shutdown_data
,
177 .service_id
= "domain-panic",
178 .data
= domain_panic_data
,
180 #ifdef CONFIG_HOTPLUG_CPU
182 .service_id
= "dr-cpu",
191 .service_id
= "var-config",
195 .service_id
= "var-config-backup",
200 static DEFINE_SPINLOCK(ds_lock
);
203 struct ldc_channel
*lp
;
205 #define DS_HS_START 0x01
206 #define DS_HS_DONE 0x02
213 struct ds_cap_state
*ds_states
;
216 struct ds_info
*next
;
219 static struct ds_info
*ds_info_list
;
221 static struct ds_cap_state
*find_cap(struct ds_info
*dp
, u64 handle
)
223 unsigned int index
= handle
>> 32;
225 if (index
>= dp
->num_ds_states
)
227 return &dp
->ds_states
[index
];
230 static struct ds_cap_state
*find_cap_by_string(struct ds_info
*dp
,
235 for (i
= 0; i
< dp
->num_ds_states
; i
++) {
236 if (strcmp(dp
->ds_states
[i
].service_id
, name
))
239 return &dp
->ds_states
[i
];
244 static int __ds_send(struct ldc_channel
*lp
, void *data
, int len
)
246 int err
, limit
= 1000;
249 while (limit
-- > 0) {
250 err
= ldc_write(lp
, data
, len
);
251 if (!err
|| (err
!= -EAGAIN
))
259 static int ds_send(struct ldc_channel
*lp
, void *data
, int len
)
264 spin_lock_irqsave(&ds_lock
, flags
);
265 err
= __ds_send(lp
, data
, len
);
266 spin_unlock_irqrestore(&ds_lock
, flags
);
271 struct ds_md_update_req
{
275 struct ds_md_update_res
{
280 static void md_update_data(struct ds_info
*dp
,
281 struct ds_cap_state
*cp
,
284 struct ldc_channel
*lp
= dp
->lp
;
285 struct ds_data
*dpkt
= buf
;
286 struct ds_md_update_req
*rp
;
289 struct ds_md_update_res res
;
292 rp
= (struct ds_md_update_req
*) (dpkt
+ 1);
294 printk(KERN_INFO
"ds-%llu: Machine description update.\n", dp
->id
);
298 memset(&pkt
, 0, sizeof(pkt
));
299 pkt
.data
.tag
.type
= DS_DATA
;
300 pkt
.data
.tag
.len
= sizeof(pkt
) - sizeof(struct ds_msg_tag
);
301 pkt
.data
.handle
= cp
->handle
;
302 pkt
.res
.req_num
= rp
->req_num
;
303 pkt
.res
.result
= DS_OK
;
305 ds_send(lp
, &pkt
, sizeof(pkt
));
308 struct ds_shutdown_req
{
313 struct ds_shutdown_res
{
319 static void domain_shutdown_data(struct ds_info
*dp
,
320 struct ds_cap_state
*cp
,
323 struct ldc_channel
*lp
= dp
->lp
;
324 struct ds_data
*dpkt
= buf
;
325 struct ds_shutdown_req
*rp
;
328 struct ds_shutdown_res res
;
331 rp
= (struct ds_shutdown_req
*) (dpkt
+ 1);
333 printk(KERN_ALERT
"ds-%llu: Shutdown request from "
334 "LDOM manager received.\n", dp
->id
);
336 memset(&pkt
, 0, sizeof(pkt
));
337 pkt
.data
.tag
.type
= DS_DATA
;
338 pkt
.data
.tag
.len
= sizeof(pkt
) - sizeof(struct ds_msg_tag
);
339 pkt
.data
.handle
= cp
->handle
;
340 pkt
.res
.req_num
= rp
->req_num
;
341 pkt
.res
.result
= DS_OK
;
342 pkt
.res
.reason
[0] = 0;
344 ds_send(lp
, &pkt
, sizeof(pkt
));
346 orderly_poweroff(true);
349 struct ds_panic_req
{
353 struct ds_panic_res
{
359 static void domain_panic_data(struct ds_info
*dp
,
360 struct ds_cap_state
*cp
,
363 struct ldc_channel
*lp
= dp
->lp
;
364 struct ds_data
*dpkt
= buf
;
365 struct ds_panic_req
*rp
;
368 struct ds_panic_res res
;
371 rp
= (struct ds_panic_req
*) (dpkt
+ 1);
373 printk(KERN_ALERT
"ds-%llu: Panic request from "
374 "LDOM manager received.\n", dp
->id
);
376 memset(&pkt
, 0, sizeof(pkt
));
377 pkt
.data
.tag
.type
= DS_DATA
;
378 pkt
.data
.tag
.len
= sizeof(pkt
) - sizeof(struct ds_msg_tag
);
379 pkt
.data
.handle
= cp
->handle
;
380 pkt
.res
.req_num
= rp
->req_num
;
381 pkt
.res
.result
= DS_OK
;
382 pkt
.res
.reason
[0] = 0;
384 ds_send(lp
, &pkt
, sizeof(pkt
));
386 panic("PANIC requested by LDOM manager.");
389 #ifdef CONFIG_HOTPLUG_CPU
393 #define DR_CPU_CONFIGURE 0x43
394 #define DR_CPU_UNCONFIGURE 0x55
395 #define DR_CPU_FORCE_UNCONFIGURE 0x46
396 #define DR_CPU_STATUS 0x53
399 #define DR_CPU_OK 0x6f
400 #define DR_CPU_ERROR 0x65
405 struct dr_cpu_resp_entry
{
408 #define DR_CPU_RES_OK 0x00
409 #define DR_CPU_RES_FAILURE 0x01
410 #define DR_CPU_RES_BLOCKED 0x02
411 #define DR_CPU_RES_CPU_NOT_RESPONDING 0x03
412 #define DR_CPU_RES_NOT_IN_MD 0x04
415 #define DR_CPU_STAT_NOT_PRESENT 0x00
416 #define DR_CPU_STAT_UNCONFIGURED 0x01
417 #define DR_CPU_STAT_CONFIGURED 0x02
422 static void __dr_cpu_send_error(struct ds_info
*dp
,
423 struct ds_cap_state
*cp
,
424 struct ds_data
*data
)
426 struct dr_cpu_tag
*tag
= (struct dr_cpu_tag
*) (data
+ 1);
429 struct dr_cpu_tag tag
;
433 memset(&pkt
, 0, sizeof(pkt
));
434 pkt
.data
.tag
.type
= DS_DATA
;
435 pkt
.data
.handle
= cp
->handle
;
436 pkt
.tag
.req_num
= tag
->req_num
;
437 pkt
.tag
.type
= DR_CPU_ERROR
;
438 pkt
.tag
.num_records
= 0;
440 msg_len
= (sizeof(struct ds_data
) +
441 sizeof(struct dr_cpu_tag
));
443 pkt
.data
.tag
.len
= msg_len
- sizeof(struct ds_msg_tag
);
445 __ds_send(dp
->lp
, &pkt
, msg_len
);
448 static void dr_cpu_send_error(struct ds_info
*dp
,
449 struct ds_cap_state
*cp
,
450 struct ds_data
*data
)
454 spin_lock_irqsave(&ds_lock
, flags
);
455 __dr_cpu_send_error(dp
, cp
, data
);
456 spin_unlock_irqrestore(&ds_lock
, flags
);
459 #define CPU_SENTINEL 0xffffffff
461 static void purge_dups(u32
*list
, u32 num_ents
)
465 for (i
= 0; i
< num_ents
; i
++) {
469 if (cpu
== CPU_SENTINEL
)
472 for (j
= i
+ 1; j
< num_ents
; j
++) {
474 list
[j
] = CPU_SENTINEL
;
479 static int dr_cpu_size_response(int ncpus
)
481 return (sizeof(struct ds_data
) +
482 sizeof(struct dr_cpu_tag
) +
483 (sizeof(struct dr_cpu_resp_entry
) * ncpus
));
486 static void dr_cpu_init_response(struct ds_data
*resp
, u64 req_num
,
487 u64 handle
, int resp_len
, int ncpus
,
488 cpumask_t
*mask
, u32 default_stat
)
490 struct dr_cpu_resp_entry
*ent
;
491 struct dr_cpu_tag
*tag
;
494 tag
= (struct dr_cpu_tag
*) (resp
+ 1);
495 ent
= (struct dr_cpu_resp_entry
*) (tag
+ 1);
497 resp
->tag
.type
= DS_DATA
;
498 resp
->tag
.len
= resp_len
- sizeof(struct ds_msg_tag
);
499 resp
->handle
= handle
;
500 tag
->req_num
= req_num
;
501 tag
->type
= DR_CPU_OK
;
502 tag
->num_records
= ncpus
;
505 for_each_cpu(cpu
, mask
) {
507 ent
[i
].result
= DR_CPU_RES_OK
;
508 ent
[i
].stat
= default_stat
;
514 static void dr_cpu_mark(struct ds_data
*resp
, int cpu
, int ncpus
,
517 struct dr_cpu_resp_entry
*ent
;
518 struct dr_cpu_tag
*tag
;
521 tag
= (struct dr_cpu_tag
*) (resp
+ 1);
522 ent
= (struct dr_cpu_resp_entry
*) (tag
+ 1);
524 for (i
= 0; i
< ncpus
; i
++) {
525 if (ent
[i
].cpu
!= cpu
)
533 static int dr_cpu_configure(struct ds_info
*dp
, struct ds_cap_state
*cp
,
534 u64 req_num
, cpumask_t
*mask
)
536 struct ds_data
*resp
;
537 int resp_len
, ncpus
, cpu
;
540 ncpus
= cpumask_weight(mask
);
541 resp_len
= dr_cpu_size_response(ncpus
);
542 resp
= kzalloc(resp_len
, GFP_KERNEL
);
546 dr_cpu_init_response(resp
, req_num
, cp
->handle
,
547 resp_len
, ncpus
, mask
,
548 DR_CPU_STAT_CONFIGURED
);
550 mdesc_populate_present_mask(mask
);
551 mdesc_fill_in_cpu_data(mask
);
553 for_each_cpu(cpu
, mask
) {
556 printk(KERN_INFO
"ds-%llu: Starting cpu %d...\n",
560 __u32 res
= DR_CPU_RES_FAILURE
;
561 __u32 stat
= DR_CPU_STAT_UNCONFIGURED
;
563 if (!cpu_present(cpu
)) {
564 /* CPU not present in MD */
565 res
= DR_CPU_RES_NOT_IN_MD
;
566 stat
= DR_CPU_STAT_NOT_PRESENT
;
567 } else if (err
== -ENODEV
) {
568 /* CPU did not call in successfully */
569 res
= DR_CPU_RES_CPU_NOT_RESPONDING
;
572 printk(KERN_INFO
"ds-%llu: CPU startup failed err=%d\n",
574 dr_cpu_mark(resp
, cpu
, ncpus
, res
, stat
);
578 spin_lock_irqsave(&ds_lock
, flags
);
579 __ds_send(dp
->lp
, resp
, resp_len
);
580 spin_unlock_irqrestore(&ds_lock
, flags
);
584 /* Redistribute IRQs, taking into account the new cpus. */
590 static int dr_cpu_unconfigure(struct ds_info
*dp
,
591 struct ds_cap_state
*cp
,
595 struct ds_data
*resp
;
596 int resp_len
, ncpus
, cpu
;
599 ncpus
= cpumask_weight(mask
);
600 resp_len
= dr_cpu_size_response(ncpus
);
601 resp
= kzalloc(resp_len
, GFP_KERNEL
);
605 dr_cpu_init_response(resp
, req_num
, cp
->handle
,
606 resp_len
, ncpus
, mask
,
607 DR_CPU_STAT_UNCONFIGURED
);
609 for_each_cpu(cpu
, mask
) {
612 printk(KERN_INFO
"ds-%llu: Shutting down cpu %d...\n",
616 dr_cpu_mark(resp
, cpu
, ncpus
,
618 DR_CPU_STAT_CONFIGURED
);
621 spin_lock_irqsave(&ds_lock
, flags
);
622 __ds_send(dp
->lp
, resp
, resp_len
);
623 spin_unlock_irqrestore(&ds_lock
, flags
);
630 static void dr_cpu_data(struct ds_info
*dp
, struct ds_cap_state
*cp
, void *buf
,
633 struct ds_data
*data
= buf
;
634 struct dr_cpu_tag
*tag
= (struct dr_cpu_tag
*) (data
+ 1);
635 u32
*cpu_list
= (u32
*) (tag
+ 1);
636 u64 req_num
= tag
->req_num
;
642 case DR_CPU_CONFIGURE
:
643 case DR_CPU_UNCONFIGURE
:
644 case DR_CPU_FORCE_UNCONFIGURE
:
648 dr_cpu_send_error(dp
, cp
, data
);
652 purge_dups(cpu_list
, tag
->num_records
);
654 cpumask_clear(&mask
);
655 for (i
= 0; i
< tag
->num_records
; i
++) {
656 if (cpu_list
[i
] == CPU_SENTINEL
)
659 if (cpu_list
[i
] < nr_cpu_ids
)
660 cpumask_set_cpu(cpu_list
[i
], &mask
);
663 if (tag
->type
== DR_CPU_CONFIGURE
)
664 err
= dr_cpu_configure(dp
, cp
, req_num
, &mask
);
666 err
= dr_cpu_unconfigure(dp
, cp
, req_num
, &mask
);
669 dr_cpu_send_error(dp
, cp
, data
);
671 #endif /* CONFIG_HOTPLUG_CPU */
676 #define DS_PRI_REQUEST 0x00
677 #define DS_PRI_DATA 0x01
678 #define DS_PRI_UPDATE 0x02
681 static void ds_pri_data(struct ds_info
*dp
,
682 struct ds_cap_state
*cp
,
685 struct ds_data
*dpkt
= buf
;
686 struct ds_pri_msg
*rp
;
688 rp
= (struct ds_pri_msg
*) (dpkt
+ 1);
690 printk(KERN_INFO
"ds-%llu: PRI REQ [%llx:%llx], len=%d\n",
691 dp
->id
, rp
->req_num
, rp
->type
, len
);
696 #define DS_VAR_SET_REQ 0x00
697 #define DS_VAR_DELETE_REQ 0x01
698 #define DS_VAR_SET_RESP 0x02
699 #define DS_VAR_DELETE_RESP 0x03
702 struct ds_var_set_msg
{
703 struct ds_var_hdr hdr
;
704 char name_and_value
[0];
707 struct ds_var_delete_msg
{
708 struct ds_var_hdr hdr
;
713 struct ds_var_hdr hdr
;
715 #define DS_VAR_SUCCESS 0x00
716 #define DS_VAR_NO_SPACE 0x01
717 #define DS_VAR_INVALID_VAR 0x02
718 #define DS_VAR_INVALID_VAL 0x03
719 #define DS_VAR_NOT_PRESENT 0x04
722 static DEFINE_MUTEX(ds_var_mutex
);
723 static int ds_var_doorbell
;
724 static int ds_var_response
;
726 static void ds_var_data(struct ds_info
*dp
,
727 struct ds_cap_state
*cp
,
730 struct ds_data
*dpkt
= buf
;
731 struct ds_var_resp
*rp
;
733 rp
= (struct ds_var_resp
*) (dpkt
+ 1);
735 if (rp
->hdr
.type
!= DS_VAR_SET_RESP
&&
736 rp
->hdr
.type
!= DS_VAR_DELETE_RESP
)
739 ds_var_response
= rp
->result
;
744 void ldom_set_var(const char *var
, const char *value
)
746 struct ds_cap_state
*cp
;
750 spin_lock_irqsave(&ds_lock
, flags
);
752 for (dp
= ds_info_list
; dp
; dp
= dp
->next
) {
753 struct ds_cap_state
*tmp
;
755 tmp
= find_cap_by_string(dp
, "var-config");
756 if (tmp
&& tmp
->state
== CAP_STATE_REGISTERED
) {
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-backup");
766 if (tmp
&& tmp
->state
== CAP_STATE_REGISTERED
) {
772 spin_unlock_irqrestore(&ds_lock
, flags
);
778 struct ds_var_set_msg msg
;
785 if (strlen(var
) + strlen(value
) + 2 >
786 sizeof(pkt
) - sizeof(pkt
.header
)) {
788 "contents length: %zu, which more than max: %lu,"
789 "so could not set (%s) variable to (%s).\n",
790 strlen(var
) + strlen(value
) + 2,
791 sizeof(pkt
) - sizeof(pkt
.header
), var
, value
);
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];
801 p
+= strlen(var
) + 1;
803 p
+= strlen(value
) + 1;
805 msg_len
= (sizeof(struct ds_data
) +
806 sizeof(struct ds_var_set_msg
) +
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
);
815 ds_var_response
= -1;
817 __ds_send(dp
->lp
, &pkt
, msg_len
);
818 spin_unlock_irqrestore(&ds_lock
, flags
);
821 while (ds_var_doorbell
== 0) {
828 mutex_unlock(&ds_var_mutex
);
830 if (ds_var_doorbell
== 0 ||
831 ds_var_response
!= DS_VAR_SUCCESS
)
832 printk(KERN_ERR
"ds-%llu: var-config [%s:%s] "
833 "failed, response(%d).\n",
837 printk(KERN_ERR PFX
"var-config not registered so "
838 "could not set (%s) variable to (%s).\n",
843 static char full_boot_str
[256] __attribute__((aligned(32)));
844 static int reboot_data_supported
;
846 void ldom_reboot(const char *boot_command
)
848 /* Don't bother with any of this if the boot_command
851 if (boot_command
&& strlen(boot_command
)) {
854 snprintf(full_boot_str
, sizeof(full_boot_str
), "boot %s",
856 len
= strlen(full_boot_str
);
858 if (reboot_data_supported
) {
859 unsigned long ra
= kimage_addr_to_ra(full_boot_str
);
860 unsigned long hv_ret
;
862 hv_ret
= sun4v_reboot_data_set(ra
, len
);
863 if (hv_ret
!= HV_EOK
)
864 pr_err("SUN4V: Unable to set reboot data "
865 "hv_ret=%lu\n", hv_ret
);
867 ldom_set_var("reboot-command", full_boot_str
);
873 void ldom_power_off(void)
878 static void ds_conn_reset(struct ds_info
*dp
)
880 printk(KERN_ERR
"ds-%llu: ds_conn_reset() from %ps\n",
881 dp
->id
, __builtin_return_address(0));
884 static int register_services(struct ds_info
*dp
)
886 struct ldc_channel
*lp
= dp
->lp
;
889 for (i
= 0; i
< dp
->num_ds_states
; i
++) {
891 struct ds_reg_req req
;
894 struct ds_cap_state
*cp
= &dp
->ds_states
[i
];
898 if (cp
->state
== CAP_STATE_REGISTERED
)
901 new_count
= sched_clock() & 0xffffffff;
902 cp
->handle
= ((u64
) i
<< 32) | new_count
;
904 msg_len
= (sizeof(struct ds_reg_req
) +
905 strlen(cp
->service_id
));
907 memset(&pbuf
, 0, sizeof(pbuf
));
908 pbuf
.req
.tag
.type
= DS_REG_REQ
;
909 pbuf
.req
.tag
.len
= (msg_len
- sizeof(struct ds_msg_tag
));
910 pbuf
.req
.handle
= cp
->handle
;
913 strcpy(pbuf
.id_buf
, cp
->service_id
);
915 err
= __ds_send(lp
, &pbuf
, msg_len
);
917 cp
->state
= CAP_STATE_REG_SENT
;
922 static int ds_handshake(struct ds_info
*dp
, struct ds_msg_tag
*pkt
)
925 if (dp
->hs_state
== DS_HS_START
) {
926 if (pkt
->type
!= DS_INIT_ACK
)
929 dp
->hs_state
= DS_HS_DONE
;
931 return register_services(dp
);
934 if (dp
->hs_state
!= DS_HS_DONE
)
937 if (pkt
->type
== DS_REG_ACK
) {
938 struct ds_reg_ack
*ap
= (struct ds_reg_ack
*) pkt
;
939 struct ds_cap_state
*cp
= find_cap(dp
, ap
->handle
);
942 printk(KERN_ERR
"ds-%llu: REG ACK for unknown "
943 "handle %llx\n", dp
->id
, ap
->handle
);
946 printk(KERN_INFO
"ds-%llu: Registered %s service.\n",
947 dp
->id
, cp
->service_id
);
948 cp
->state
= CAP_STATE_REGISTERED
;
949 } else if (pkt
->type
== DS_REG_NACK
) {
950 struct ds_reg_nack
*np
= (struct ds_reg_nack
*) pkt
;
951 struct ds_cap_state
*cp
= find_cap(dp
, np
->handle
);
954 printk(KERN_ERR
"ds-%llu: REG NACK for "
955 "unknown handle %llx\n",
959 cp
->state
= CAP_STATE_UNKNOWN
;
969 static void __send_ds_nack(struct ds_info
*dp
, u64 handle
)
971 struct ds_data_nack nack
= {
974 .len
= (sizeof(struct ds_data_nack
) -
975 sizeof(struct ds_msg_tag
)),
978 .result
= DS_INV_HDL
,
981 __ds_send(dp
->lp
, &nack
, sizeof(nack
));
984 static LIST_HEAD(ds_work_list
);
985 static DECLARE_WAIT_QUEUE_HEAD(ds_wait
);
987 struct ds_queue_entry
{
988 struct list_head list
;
995 static void process_ds_work(void)
997 struct ds_queue_entry
*qp
, *tmp
;
1001 spin_lock_irqsave(&ds_lock
, flags
);
1002 list_splice_init(&ds_work_list
, &todo
);
1003 spin_unlock_irqrestore(&ds_lock
, flags
);
1005 list_for_each_entry_safe(qp
, tmp
, &todo
, list
) {
1006 struct ds_data
*dpkt
= (struct ds_data
*) qp
->req
;
1007 struct ds_info
*dp
= qp
->dp
;
1008 struct ds_cap_state
*cp
= find_cap(dp
, dpkt
->handle
);
1009 int req_len
= qp
->req_len
;
1012 printk(KERN_ERR
"ds-%llu: Data for unknown "
1014 dp
->id
, dpkt
->handle
);
1016 spin_lock_irqsave(&ds_lock
, flags
);
1017 __send_ds_nack(dp
, dpkt
->handle
);
1018 spin_unlock_irqrestore(&ds_lock
, flags
);
1020 cp
->data(dp
, cp
, dpkt
, req_len
);
1023 list_del(&qp
->list
);
1028 static int ds_thread(void *__unused
)
1033 prepare_to_wait(&ds_wait
, &wait
, TASK_INTERRUPTIBLE
);
1034 if (list_empty(&ds_work_list
))
1036 finish_wait(&ds_wait
, &wait
);
1038 if (kthread_should_stop())
1047 static int ds_data(struct ds_info
*dp
, struct ds_msg_tag
*pkt
, int len
)
1049 struct ds_data
*dpkt
= (struct ds_data
*) pkt
;
1050 struct ds_queue_entry
*qp
;
1052 qp
= kmalloc(sizeof(struct ds_queue_entry
) + len
, GFP_ATOMIC
);
1054 __send_ds_nack(dp
, dpkt
->handle
);
1057 memcpy(&qp
->req
, pkt
, len
);
1058 list_add_tail(&qp
->list
, &ds_work_list
);
1064 static void ds_up(struct ds_info
*dp
)
1066 struct ldc_channel
*lp
= dp
->lp
;
1067 struct ds_ver_req req
;
1070 req
.tag
.type
= DS_INIT_REQ
;
1071 req
.tag
.len
= sizeof(req
) - sizeof(struct ds_msg_tag
);
1075 err
= __ds_send(lp
, &req
, sizeof(req
));
1077 dp
->hs_state
= DS_HS_START
;
1080 static void ds_reset(struct ds_info
*dp
)
1086 for (i
= 0; i
< dp
->num_ds_states
; i
++) {
1087 struct ds_cap_state
*cp
= &dp
->ds_states
[i
];
1089 cp
->state
= CAP_STATE_UNKNOWN
;
1093 static void ds_event(void *arg
, int event
)
1095 struct ds_info
*dp
= arg
;
1096 struct ldc_channel
*lp
= dp
->lp
;
1097 unsigned long flags
;
1100 spin_lock_irqsave(&ds_lock
, flags
);
1102 if (event
== LDC_EVENT_UP
) {
1104 spin_unlock_irqrestore(&ds_lock
, flags
);
1108 if (event
== LDC_EVENT_RESET
) {
1110 spin_unlock_irqrestore(&ds_lock
, flags
);
1114 if (event
!= LDC_EVENT_DATA_READY
) {
1115 printk(KERN_WARNING
"ds-%llu: Unexpected LDC event %d\n",
1117 spin_unlock_irqrestore(&ds_lock
, flags
);
1123 struct ds_msg_tag
*tag
;
1125 err
= ldc_read(lp
, dp
->rcv_buf
, sizeof(*tag
));
1127 if (unlikely(err
< 0)) {
1128 if (err
== -ECONNRESET
)
1136 err
= ldc_read(lp
, tag
+ 1, tag
->len
);
1138 if (unlikely(err
< 0)) {
1139 if (err
== -ECONNRESET
)
1146 if (tag
->type
< DS_DATA
)
1147 err
= ds_handshake(dp
, dp
->rcv_buf
);
1149 err
= ds_data(dp
, dp
->rcv_buf
,
1150 sizeof(*tag
) + err
);
1151 if (err
== -ECONNRESET
)
1155 spin_unlock_irqrestore(&ds_lock
, flags
);
1158 static int ds_probe(struct vio_dev
*vdev
, const struct vio_device_id
*id
)
1160 static int ds_version_printed
;
1161 struct ldc_channel_config ds_cfg
= {
1164 .mode
= LDC_MODE_STREAM
,
1166 struct mdesc_handle
*hp
;
1167 struct ldc_channel
*lp
;
1172 if (ds_version_printed
++ == 0)
1173 printk(KERN_INFO
"%s", version
);
1175 dp
= kzalloc(sizeof(*dp
), GFP_KERNEL
);
1181 val
= mdesc_get_property(hp
, vdev
->mp
, "id", NULL
);
1186 dp
->rcv_buf
= kzalloc(4096, GFP_KERNEL
);
1190 dp
->rcv_buf_len
= 4096;
1192 dp
->ds_states
= kmemdup(ds_states_template
,
1193 sizeof(ds_states_template
), GFP_KERNEL
);
1195 goto out_free_rcv_buf
;
1197 dp
->num_ds_states
= ARRAY_SIZE(ds_states_template
);
1199 for (i
= 0; i
< dp
->num_ds_states
; i
++)
1200 dp
->ds_states
[i
].handle
= ((u64
)i
<< 32);
1202 ds_cfg
.tx_irq
= vdev
->tx_irq
;
1203 ds_cfg
.rx_irq
= vdev
->rx_irq
;
1205 lp
= ldc_alloc(vdev
->channel_id
, &ds_cfg
, dp
, "DS");
1208 goto out_free_ds_states
;
1216 spin_lock_irq(&ds_lock
);
1217 dp
->next
= ds_info_list
;
1219 spin_unlock_irq(&ds_lock
);
1227 kfree(dp
->ds_states
);
1239 static int ds_remove(struct vio_dev
*vdev
)
1244 static const struct vio_device_id ds_match
[] = {
1246 .type
= "domain-services-port",
1251 static struct vio_driver ds_driver
= {
1252 .id_table
= ds_match
,
1254 .remove
= ds_remove
,
1258 static int __init
ds_init(void)
1260 unsigned long hv_ret
, major
, minor
;
1262 if (tlb_type
== hypervisor
) {
1263 hv_ret
= sun4v_get_version(HV_GRP_REBOOT_DATA
, &major
, &minor
);
1264 if (hv_ret
== HV_EOK
) {
1265 pr_info("SUN4V: Reboot data supported (maj=%lu,min=%lu).\n",
1267 reboot_data_supported
= 1;
1270 kthread_run(ds_thread
, NULL
, "kldomd");
1272 return vio_register_driver(&ds_driver
);
1275 fs_initcall(ds_init
);