1 /* ds.c: Domain Services driver for Logical Domains
3 * Copyright (C) 2007, 2008 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/sched/clock.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>
19 #include <asm/hypervisor.h>
22 #include <asm/mdesc.h>
28 #define DRV_MODULE_NAME "ds"
29 #define PFX DRV_MODULE_NAME ": "
30 #define DRV_MODULE_VERSION "1.0"
31 #define DRV_MODULE_RELDATE "Jul 11, 2007"
33 static char version
[] =
34 DRV_MODULE_NAME
".c:v" DRV_MODULE_VERSION
" (" DRV_MODULE_RELDATE
")\n";
35 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
36 MODULE_DESCRIPTION("Sun LDOM domain services driver");
37 MODULE_LICENSE("GPL");
38 MODULE_VERSION(DRV_MODULE_VERSION
);
42 #define DS_INIT_REQ 0x00
43 #define DS_INIT_ACK 0x01
44 #define DS_INIT_NACK 0x02
45 #define DS_REG_REQ 0x03
46 #define DS_REG_ACK 0x04
47 #define DS_REG_NACK 0x05
48 #define DS_UNREG_REQ 0x06
49 #define DS_UNREG_ACK 0x07
50 #define DS_UNREG_NACK 0x08
59 #define DS_REG_VER_NACK 0x01
60 #define DS_REG_DUP 0x02
61 #define DS_INV_HDL 0x03
62 #define DS_TYPE_UNKNOWN 0x04
70 struct ds_msg_tag tag
;
71 struct ds_version ver
;
75 struct ds_msg_tag tag
;
80 struct ds_msg_tag tag
;
85 struct ds_msg_tag tag
;
93 struct ds_msg_tag tag
;
99 struct ds_msg_tag tag
;
104 struct ds_unreg_req
{
105 struct ds_msg_tag tag
;
109 struct ds_unreg_ack
{
110 struct ds_msg_tag tag
;
114 struct ds_unreg_nack
{
115 struct ds_msg_tag tag
;
120 struct ds_msg_tag tag
;
124 struct ds_data_nack
{
125 struct ds_msg_tag tag
;
131 struct ds_cap_state
{
134 void (*data
)(struct ds_info
*dp
,
135 struct ds_cap_state
*cp
,
138 const char *service_id
;
141 #define CAP_STATE_UNKNOWN 0x00
142 #define CAP_STATE_REG_SENT 0x01
143 #define CAP_STATE_REGISTERED 0x02
146 static void md_update_data(struct ds_info
*dp
, struct ds_cap_state
*cp
,
148 static void domain_shutdown_data(struct ds_info
*dp
,
149 struct ds_cap_state
*cp
,
151 static void domain_panic_data(struct ds_info
*dp
,
152 struct ds_cap_state
*cp
,
154 #ifdef CONFIG_HOTPLUG_CPU
155 static void dr_cpu_data(struct ds_info
*dp
,
156 struct ds_cap_state
*cp
,
159 static void ds_pri_data(struct ds_info
*dp
,
160 struct ds_cap_state
*cp
,
162 static void ds_var_data(struct ds_info
*dp
,
163 struct ds_cap_state
*cp
,
166 static struct ds_cap_state ds_states_template
[] = {
168 .service_id
= "md-update",
169 .data
= md_update_data
,
172 .service_id
= "domain-shutdown",
173 .data
= domain_shutdown_data
,
176 .service_id
= "domain-panic",
177 .data
= domain_panic_data
,
179 #ifdef CONFIG_HOTPLUG_CPU
181 .service_id
= "dr-cpu",
190 .service_id
= "var-config",
194 .service_id
= "var-config-backup",
199 static DEFINE_SPINLOCK(ds_lock
);
202 struct ldc_channel
*lp
;
204 #define DS_HS_START 0x01
205 #define DS_HS_DONE 0x02
212 struct ds_cap_state
*ds_states
;
215 struct ds_info
*next
;
218 static struct ds_info
*ds_info_list
;
220 static struct ds_cap_state
*find_cap(struct ds_info
*dp
, u64 handle
)
222 unsigned int index
= handle
>> 32;
224 if (index
>= dp
->num_ds_states
)
226 return &dp
->ds_states
[index
];
229 static struct ds_cap_state
*find_cap_by_string(struct ds_info
*dp
,
234 for (i
= 0; i
< dp
->num_ds_states
; i
++) {
235 if (strcmp(dp
->ds_states
[i
].service_id
, name
))
238 return &dp
->ds_states
[i
];
243 static int __ds_send(struct ldc_channel
*lp
, void *data
, int len
)
245 int err
, limit
= 1000;
248 while (limit
-- > 0) {
249 err
= ldc_write(lp
, data
, len
);
250 if (!err
|| (err
!= -EAGAIN
))
258 static int ds_send(struct ldc_channel
*lp
, void *data
, int len
)
263 spin_lock_irqsave(&ds_lock
, flags
);
264 err
= __ds_send(lp
, data
, len
);
265 spin_unlock_irqrestore(&ds_lock
, flags
);
270 struct ds_md_update_req
{
274 struct ds_md_update_res
{
279 static void md_update_data(struct ds_info
*dp
,
280 struct ds_cap_state
*cp
,
283 struct ldc_channel
*lp
= dp
->lp
;
284 struct ds_data
*dpkt
= buf
;
285 struct ds_md_update_req
*rp
;
288 struct ds_md_update_res res
;
291 rp
= (struct ds_md_update_req
*) (dpkt
+ 1);
293 printk(KERN_INFO
"ds-%llu: Machine description update.\n", dp
->id
);
297 memset(&pkt
, 0, sizeof(pkt
));
298 pkt
.data
.tag
.type
= DS_DATA
;
299 pkt
.data
.tag
.len
= sizeof(pkt
) - sizeof(struct ds_msg_tag
);
300 pkt
.data
.handle
= cp
->handle
;
301 pkt
.res
.req_num
= rp
->req_num
;
302 pkt
.res
.result
= DS_OK
;
304 ds_send(lp
, &pkt
, sizeof(pkt
));
307 struct ds_shutdown_req
{
312 struct ds_shutdown_res
{
318 static void domain_shutdown_data(struct ds_info
*dp
,
319 struct ds_cap_state
*cp
,
322 struct ldc_channel
*lp
= dp
->lp
;
323 struct ds_data
*dpkt
= buf
;
324 struct ds_shutdown_req
*rp
;
327 struct ds_shutdown_res res
;
330 rp
= (struct ds_shutdown_req
*) (dpkt
+ 1);
332 printk(KERN_ALERT
"ds-%llu: Shutdown request from "
333 "LDOM manager received.\n", dp
->id
);
335 memset(&pkt
, 0, sizeof(pkt
));
336 pkt
.data
.tag
.type
= DS_DATA
;
337 pkt
.data
.tag
.len
= sizeof(pkt
) - sizeof(struct ds_msg_tag
);
338 pkt
.data
.handle
= cp
->handle
;
339 pkt
.res
.req_num
= rp
->req_num
;
340 pkt
.res
.result
= DS_OK
;
341 pkt
.res
.reason
[0] = 0;
343 ds_send(lp
, &pkt
, sizeof(pkt
));
345 orderly_poweroff(true);
348 struct ds_panic_req
{
352 struct ds_panic_res
{
358 static void domain_panic_data(struct ds_info
*dp
,
359 struct ds_cap_state
*cp
,
362 struct ldc_channel
*lp
= dp
->lp
;
363 struct ds_data
*dpkt
= buf
;
364 struct ds_panic_req
*rp
;
367 struct ds_panic_res res
;
370 rp
= (struct ds_panic_req
*) (dpkt
+ 1);
372 printk(KERN_ALERT
"ds-%llu: Panic request from "
373 "LDOM manager received.\n", dp
->id
);
375 memset(&pkt
, 0, sizeof(pkt
));
376 pkt
.data
.tag
.type
= DS_DATA
;
377 pkt
.data
.tag
.len
= sizeof(pkt
) - sizeof(struct ds_msg_tag
);
378 pkt
.data
.handle
= cp
->handle
;
379 pkt
.res
.req_num
= rp
->req_num
;
380 pkt
.res
.result
= DS_OK
;
381 pkt
.res
.reason
[0] = 0;
383 ds_send(lp
, &pkt
, sizeof(pkt
));
385 panic("PANIC requested by LDOM manager.");
388 #ifdef CONFIG_HOTPLUG_CPU
392 #define DR_CPU_CONFIGURE 0x43
393 #define DR_CPU_UNCONFIGURE 0x55
394 #define DR_CPU_FORCE_UNCONFIGURE 0x46
395 #define DR_CPU_STATUS 0x53
398 #define DR_CPU_OK 0x6f
399 #define DR_CPU_ERROR 0x65
404 struct dr_cpu_resp_entry
{
407 #define DR_CPU_RES_OK 0x00
408 #define DR_CPU_RES_FAILURE 0x01
409 #define DR_CPU_RES_BLOCKED 0x02
410 #define DR_CPU_RES_CPU_NOT_RESPONDING 0x03
411 #define DR_CPU_RES_NOT_IN_MD 0x04
414 #define DR_CPU_STAT_NOT_PRESENT 0x00
415 #define DR_CPU_STAT_UNCONFIGURED 0x01
416 #define DR_CPU_STAT_CONFIGURED 0x02
421 static void __dr_cpu_send_error(struct ds_info
*dp
,
422 struct ds_cap_state
*cp
,
423 struct ds_data
*data
)
425 struct dr_cpu_tag
*tag
= (struct dr_cpu_tag
*) (data
+ 1);
428 struct dr_cpu_tag tag
;
432 memset(&pkt
, 0, sizeof(pkt
));
433 pkt
.data
.tag
.type
= DS_DATA
;
434 pkt
.data
.handle
= cp
->handle
;
435 pkt
.tag
.req_num
= tag
->req_num
;
436 pkt
.tag
.type
= DR_CPU_ERROR
;
437 pkt
.tag
.num_records
= 0;
439 msg_len
= (sizeof(struct ds_data
) +
440 sizeof(struct dr_cpu_tag
));
442 pkt
.data
.tag
.len
= msg_len
- sizeof(struct ds_msg_tag
);
444 __ds_send(dp
->lp
, &pkt
, msg_len
);
447 static void dr_cpu_send_error(struct ds_info
*dp
,
448 struct ds_cap_state
*cp
,
449 struct ds_data
*data
)
453 spin_lock_irqsave(&ds_lock
, flags
);
454 __dr_cpu_send_error(dp
, cp
, data
);
455 spin_unlock_irqrestore(&ds_lock
, flags
);
458 #define CPU_SENTINEL 0xffffffff
460 static void purge_dups(u32
*list
, u32 num_ents
)
464 for (i
= 0; i
< num_ents
; i
++) {
468 if (cpu
== CPU_SENTINEL
)
471 for (j
= i
+ 1; j
< num_ents
; j
++) {
473 list
[j
] = CPU_SENTINEL
;
478 static int dr_cpu_size_response(int ncpus
)
480 return (sizeof(struct ds_data
) +
481 sizeof(struct dr_cpu_tag
) +
482 (sizeof(struct dr_cpu_resp_entry
) * ncpus
));
485 static void dr_cpu_init_response(struct ds_data
*resp
, u64 req_num
,
486 u64 handle
, int resp_len
, int ncpus
,
487 cpumask_t
*mask
, u32 default_stat
)
489 struct dr_cpu_resp_entry
*ent
;
490 struct dr_cpu_tag
*tag
;
493 tag
= (struct dr_cpu_tag
*) (resp
+ 1);
494 ent
= (struct dr_cpu_resp_entry
*) (tag
+ 1);
496 resp
->tag
.type
= DS_DATA
;
497 resp
->tag
.len
= resp_len
- sizeof(struct ds_msg_tag
);
498 resp
->handle
= handle
;
499 tag
->req_num
= req_num
;
500 tag
->type
= DR_CPU_OK
;
501 tag
->num_records
= ncpus
;
504 for_each_cpu(cpu
, mask
) {
506 ent
[i
].result
= DR_CPU_RES_OK
;
507 ent
[i
].stat
= default_stat
;
513 static void dr_cpu_mark(struct ds_data
*resp
, int cpu
, int ncpus
,
516 struct dr_cpu_resp_entry
*ent
;
517 struct dr_cpu_tag
*tag
;
520 tag
= (struct dr_cpu_tag
*) (resp
+ 1);
521 ent
= (struct dr_cpu_resp_entry
*) (tag
+ 1);
523 for (i
= 0; i
< ncpus
; i
++) {
524 if (ent
[i
].cpu
!= cpu
)
532 static int dr_cpu_configure(struct ds_info
*dp
, struct ds_cap_state
*cp
,
533 u64 req_num
, cpumask_t
*mask
)
535 struct ds_data
*resp
;
536 int resp_len
, ncpus
, cpu
;
539 ncpus
= cpumask_weight(mask
);
540 resp_len
= dr_cpu_size_response(ncpus
);
541 resp
= kzalloc(resp_len
, GFP_KERNEL
);
545 dr_cpu_init_response(resp
, req_num
, cp
->handle
,
546 resp_len
, ncpus
, mask
,
547 DR_CPU_STAT_CONFIGURED
);
549 mdesc_populate_present_mask(mask
);
550 mdesc_fill_in_cpu_data(mask
);
552 for_each_cpu(cpu
, mask
) {
555 printk(KERN_INFO
"ds-%llu: Starting cpu %d...\n",
559 __u32 res
= DR_CPU_RES_FAILURE
;
560 __u32 stat
= DR_CPU_STAT_UNCONFIGURED
;
562 if (!cpu_present(cpu
)) {
563 /* CPU not present in MD */
564 res
= DR_CPU_RES_NOT_IN_MD
;
565 stat
= DR_CPU_STAT_NOT_PRESENT
;
566 } else if (err
== -ENODEV
) {
567 /* CPU did not call in successfully */
568 res
= DR_CPU_RES_CPU_NOT_RESPONDING
;
571 printk(KERN_INFO
"ds-%llu: CPU startup failed err=%d\n",
573 dr_cpu_mark(resp
, cpu
, ncpus
, res
, stat
);
577 spin_lock_irqsave(&ds_lock
, flags
);
578 __ds_send(dp
->lp
, resp
, resp_len
);
579 spin_unlock_irqrestore(&ds_lock
, flags
);
583 /* Redistribute IRQs, taking into account the new cpus. */
589 static int dr_cpu_unconfigure(struct ds_info
*dp
,
590 struct ds_cap_state
*cp
,
594 struct ds_data
*resp
;
595 int resp_len
, ncpus
, cpu
;
598 ncpus
= cpumask_weight(mask
);
599 resp_len
= dr_cpu_size_response(ncpus
);
600 resp
= kzalloc(resp_len
, GFP_KERNEL
);
604 dr_cpu_init_response(resp
, req_num
, cp
->handle
,
605 resp_len
, ncpus
, mask
,
606 DR_CPU_STAT_UNCONFIGURED
);
608 for_each_cpu(cpu
, mask
) {
611 printk(KERN_INFO
"ds-%llu: Shutting down cpu %d...\n",
615 dr_cpu_mark(resp
, cpu
, ncpus
,
617 DR_CPU_STAT_CONFIGURED
);
620 spin_lock_irqsave(&ds_lock
, flags
);
621 __ds_send(dp
->lp
, resp
, resp_len
);
622 spin_unlock_irqrestore(&ds_lock
, flags
);
629 static void dr_cpu_data(struct ds_info
*dp
, struct ds_cap_state
*cp
, void *buf
,
632 struct ds_data
*data
= buf
;
633 struct dr_cpu_tag
*tag
= (struct dr_cpu_tag
*) (data
+ 1);
634 u32
*cpu_list
= (u32
*) (tag
+ 1);
635 u64 req_num
= tag
->req_num
;
641 case DR_CPU_CONFIGURE
:
642 case DR_CPU_UNCONFIGURE
:
643 case DR_CPU_FORCE_UNCONFIGURE
:
647 dr_cpu_send_error(dp
, cp
, data
);
651 purge_dups(cpu_list
, tag
->num_records
);
653 cpumask_clear(&mask
);
654 for (i
= 0; i
< tag
->num_records
; i
++) {
655 if (cpu_list
[i
] == CPU_SENTINEL
)
658 if (cpu_list
[i
] < nr_cpu_ids
)
659 cpumask_set_cpu(cpu_list
[i
], &mask
);
662 if (tag
->type
== DR_CPU_CONFIGURE
)
663 err
= dr_cpu_configure(dp
, cp
, req_num
, &mask
);
665 err
= dr_cpu_unconfigure(dp
, cp
, req_num
, &mask
);
668 dr_cpu_send_error(dp
, cp
, data
);
670 #endif /* CONFIG_HOTPLUG_CPU */
675 #define DS_PRI_REQUEST 0x00
676 #define DS_PRI_DATA 0x01
677 #define DS_PRI_UPDATE 0x02
680 static void ds_pri_data(struct ds_info
*dp
,
681 struct ds_cap_state
*cp
,
684 struct ds_data
*dpkt
= buf
;
685 struct ds_pri_msg
*rp
;
687 rp
= (struct ds_pri_msg
*) (dpkt
+ 1);
689 printk(KERN_INFO
"ds-%llu: PRI REQ [%llx:%llx], len=%d\n",
690 dp
->id
, rp
->req_num
, rp
->type
, len
);
695 #define DS_VAR_SET_REQ 0x00
696 #define DS_VAR_DELETE_REQ 0x01
697 #define DS_VAR_SET_RESP 0x02
698 #define DS_VAR_DELETE_RESP 0x03
701 struct ds_var_set_msg
{
702 struct ds_var_hdr hdr
;
703 char name_and_value
[0];
706 struct ds_var_delete_msg
{
707 struct ds_var_hdr hdr
;
712 struct ds_var_hdr hdr
;
714 #define DS_VAR_SUCCESS 0x00
715 #define DS_VAR_NO_SPACE 0x01
716 #define DS_VAR_INVALID_VAR 0x02
717 #define DS_VAR_INVALID_VAL 0x03
718 #define DS_VAR_NOT_PRESENT 0x04
721 static DEFINE_MUTEX(ds_var_mutex
);
722 static int ds_var_doorbell
;
723 static int ds_var_response
;
725 static void ds_var_data(struct ds_info
*dp
,
726 struct ds_cap_state
*cp
,
729 struct ds_data
*dpkt
= buf
;
730 struct ds_var_resp
*rp
;
732 rp
= (struct ds_var_resp
*) (dpkt
+ 1);
734 if (rp
->hdr
.type
!= DS_VAR_SET_RESP
&&
735 rp
->hdr
.type
!= DS_VAR_DELETE_RESP
)
738 ds_var_response
= rp
->result
;
743 void ldom_set_var(const char *var
, const char *value
)
745 struct ds_cap_state
*cp
;
749 spin_lock_irqsave(&ds_lock
, flags
);
751 for (dp
= ds_info_list
; dp
; dp
= dp
->next
) {
752 struct ds_cap_state
*tmp
;
754 tmp
= find_cap_by_string(dp
, "var-config");
755 if (tmp
&& tmp
->state
== CAP_STATE_REGISTERED
) {
761 for (dp
= ds_info_list
; dp
; dp
= dp
->next
) {
762 struct ds_cap_state
*tmp
;
764 tmp
= find_cap_by_string(dp
, "var-config-backup");
765 if (tmp
&& tmp
->state
== CAP_STATE_REGISTERED
) {
771 spin_unlock_irqrestore(&ds_lock
, flags
);
777 struct ds_var_set_msg msg
;
784 if (strlen(var
) + strlen(value
) + 2 >
785 sizeof(pkt
) - sizeof(pkt
.header
)) {
787 "contents length: %zu, which more than max: %lu,"
788 "so could not set (%s) variable to (%s).\n",
789 strlen(var
) + strlen(value
) + 2,
790 sizeof(pkt
) - sizeof(pkt
.header
), var
, value
);
794 memset(&pkt
, 0, sizeof(pkt
));
795 pkt
.header
.data
.tag
.type
= DS_DATA
;
796 pkt
.header
.data
.handle
= cp
->handle
;
797 pkt
.header
.msg
.hdr
.type
= DS_VAR_SET_REQ
;
798 base
= p
= &pkt
.header
.msg
.name_and_value
[0];
800 p
+= strlen(var
) + 1;
802 p
+= strlen(value
) + 1;
804 msg_len
= (sizeof(struct ds_data
) +
805 sizeof(struct ds_var_set_msg
) +
807 msg_len
= (msg_len
+ 3) & ~3;
808 pkt
.header
.data
.tag
.len
= msg_len
- sizeof(struct ds_msg_tag
);
810 mutex_lock(&ds_var_mutex
);
812 spin_lock_irqsave(&ds_lock
, flags
);
814 ds_var_response
= -1;
816 __ds_send(dp
->lp
, &pkt
, msg_len
);
817 spin_unlock_irqrestore(&ds_lock
, flags
);
820 while (ds_var_doorbell
== 0) {
827 mutex_unlock(&ds_var_mutex
);
829 if (ds_var_doorbell
== 0 ||
830 ds_var_response
!= DS_VAR_SUCCESS
)
831 printk(KERN_ERR
"ds-%llu: var-config [%s:%s] "
832 "failed, response(%d).\n",
836 printk(KERN_ERR PFX
"var-config not registered so "
837 "could not set (%s) variable to (%s).\n",
842 static char full_boot_str
[256] __attribute__((aligned(32)));
843 static int reboot_data_supported
;
845 void ldom_reboot(const char *boot_command
)
847 /* Don't bother with any of this if the boot_command
850 if (boot_command
&& strlen(boot_command
)) {
853 snprintf(full_boot_str
, sizeof(full_boot_str
), "boot %s",
855 len
= strlen(full_boot_str
);
857 if (reboot_data_supported
) {
858 unsigned long ra
= kimage_addr_to_ra(full_boot_str
);
859 unsigned long hv_ret
;
861 hv_ret
= sun4v_reboot_data_set(ra
, len
);
862 if (hv_ret
!= HV_EOK
)
863 pr_err("SUN4V: Unable to set reboot data "
864 "hv_ret=%lu\n", hv_ret
);
866 ldom_set_var("reboot-command", full_boot_str
);
872 void ldom_power_off(void)
877 static void ds_conn_reset(struct ds_info
*dp
)
879 printk(KERN_ERR
"ds-%llu: ds_conn_reset() from %pf\n",
880 dp
->id
, __builtin_return_address(0));
883 static int register_services(struct ds_info
*dp
)
885 struct ldc_channel
*lp
= dp
->lp
;
888 for (i
= 0; i
< dp
->num_ds_states
; i
++) {
890 struct ds_reg_req req
;
893 struct ds_cap_state
*cp
= &dp
->ds_states
[i
];
897 if (cp
->state
== CAP_STATE_REGISTERED
)
900 new_count
= sched_clock() & 0xffffffff;
901 cp
->handle
= ((u64
) i
<< 32) | new_count
;
903 msg_len
= (sizeof(struct ds_reg_req
) +
904 strlen(cp
->service_id
));
906 memset(&pbuf
, 0, sizeof(pbuf
));
907 pbuf
.req
.tag
.type
= DS_REG_REQ
;
908 pbuf
.req
.tag
.len
= (msg_len
- sizeof(struct ds_msg_tag
));
909 pbuf
.req
.handle
= cp
->handle
;
912 strcpy(pbuf
.id_buf
, cp
->service_id
);
914 err
= __ds_send(lp
, &pbuf
, msg_len
);
916 cp
->state
= CAP_STATE_REG_SENT
;
921 static int ds_handshake(struct ds_info
*dp
, struct ds_msg_tag
*pkt
)
924 if (dp
->hs_state
== DS_HS_START
) {
925 if (pkt
->type
!= DS_INIT_ACK
)
928 dp
->hs_state
= DS_HS_DONE
;
930 return register_services(dp
);
933 if (dp
->hs_state
!= DS_HS_DONE
)
936 if (pkt
->type
== DS_REG_ACK
) {
937 struct ds_reg_ack
*ap
= (struct ds_reg_ack
*) pkt
;
938 struct ds_cap_state
*cp
= find_cap(dp
, ap
->handle
);
941 printk(KERN_ERR
"ds-%llu: REG ACK for unknown "
942 "handle %llx\n", dp
->id
, ap
->handle
);
945 printk(KERN_INFO
"ds-%llu: Registered %s service.\n",
946 dp
->id
, cp
->service_id
);
947 cp
->state
= CAP_STATE_REGISTERED
;
948 } else if (pkt
->type
== DS_REG_NACK
) {
949 struct ds_reg_nack
*np
= (struct ds_reg_nack
*) pkt
;
950 struct ds_cap_state
*cp
= find_cap(dp
, np
->handle
);
953 printk(KERN_ERR
"ds-%llu: REG NACK for "
954 "unknown handle %llx\n",
958 cp
->state
= CAP_STATE_UNKNOWN
;
968 static void __send_ds_nack(struct ds_info
*dp
, u64 handle
)
970 struct ds_data_nack nack
= {
973 .len
= (sizeof(struct ds_data_nack
) -
974 sizeof(struct ds_msg_tag
)),
977 .result
= DS_INV_HDL
,
980 __ds_send(dp
->lp
, &nack
, sizeof(nack
));
983 static LIST_HEAD(ds_work_list
);
984 static DECLARE_WAIT_QUEUE_HEAD(ds_wait
);
986 struct ds_queue_entry
{
987 struct list_head list
;
994 static void process_ds_work(void)
996 struct ds_queue_entry
*qp
, *tmp
;
1000 spin_lock_irqsave(&ds_lock
, flags
);
1001 list_splice_init(&ds_work_list
, &todo
);
1002 spin_unlock_irqrestore(&ds_lock
, flags
);
1004 list_for_each_entry_safe(qp
, tmp
, &todo
, list
) {
1005 struct ds_data
*dpkt
= (struct ds_data
*) qp
->req
;
1006 struct ds_info
*dp
= qp
->dp
;
1007 struct ds_cap_state
*cp
= find_cap(dp
, dpkt
->handle
);
1008 int req_len
= qp
->req_len
;
1011 printk(KERN_ERR
"ds-%llu: Data for unknown "
1013 dp
->id
, dpkt
->handle
);
1015 spin_lock_irqsave(&ds_lock
, flags
);
1016 __send_ds_nack(dp
, dpkt
->handle
);
1017 spin_unlock_irqrestore(&ds_lock
, flags
);
1019 cp
->data(dp
, cp
, dpkt
, req_len
);
1022 list_del(&qp
->list
);
1027 static int ds_thread(void *__unused
)
1032 prepare_to_wait(&ds_wait
, &wait
, TASK_INTERRUPTIBLE
);
1033 if (list_empty(&ds_work_list
))
1035 finish_wait(&ds_wait
, &wait
);
1037 if (kthread_should_stop())
1046 static int ds_data(struct ds_info
*dp
, struct ds_msg_tag
*pkt
, int len
)
1048 struct ds_data
*dpkt
= (struct ds_data
*) pkt
;
1049 struct ds_queue_entry
*qp
;
1051 qp
= kmalloc(sizeof(struct ds_queue_entry
) + len
, GFP_ATOMIC
);
1053 __send_ds_nack(dp
, dpkt
->handle
);
1056 memcpy(&qp
->req
, pkt
, len
);
1057 list_add_tail(&qp
->list
, &ds_work_list
);
1063 static void ds_up(struct ds_info
*dp
)
1065 struct ldc_channel
*lp
= dp
->lp
;
1066 struct ds_ver_req req
;
1069 req
.tag
.type
= DS_INIT_REQ
;
1070 req
.tag
.len
= sizeof(req
) - sizeof(struct ds_msg_tag
);
1074 err
= __ds_send(lp
, &req
, sizeof(req
));
1076 dp
->hs_state
= DS_HS_START
;
1079 static void ds_reset(struct ds_info
*dp
)
1085 for (i
= 0; i
< dp
->num_ds_states
; i
++) {
1086 struct ds_cap_state
*cp
= &dp
->ds_states
[i
];
1088 cp
->state
= CAP_STATE_UNKNOWN
;
1092 static void ds_event(void *arg
, int event
)
1094 struct ds_info
*dp
= arg
;
1095 struct ldc_channel
*lp
= dp
->lp
;
1096 unsigned long flags
;
1099 spin_lock_irqsave(&ds_lock
, flags
);
1101 if (event
== LDC_EVENT_UP
) {
1103 spin_unlock_irqrestore(&ds_lock
, flags
);
1107 if (event
== LDC_EVENT_RESET
) {
1109 spin_unlock_irqrestore(&ds_lock
, flags
);
1113 if (event
!= LDC_EVENT_DATA_READY
) {
1114 printk(KERN_WARNING
"ds-%llu: Unexpected LDC event %d\n",
1116 spin_unlock_irqrestore(&ds_lock
, flags
);
1122 struct ds_msg_tag
*tag
;
1124 err
= ldc_read(lp
, dp
->rcv_buf
, sizeof(*tag
));
1126 if (unlikely(err
< 0)) {
1127 if (err
== -ECONNRESET
)
1135 err
= ldc_read(lp
, tag
+ 1, tag
->len
);
1137 if (unlikely(err
< 0)) {
1138 if (err
== -ECONNRESET
)
1145 if (tag
->type
< DS_DATA
)
1146 err
= ds_handshake(dp
, dp
->rcv_buf
);
1148 err
= ds_data(dp
, dp
->rcv_buf
,
1149 sizeof(*tag
) + err
);
1150 if (err
== -ECONNRESET
)
1154 spin_unlock_irqrestore(&ds_lock
, flags
);
1157 static int ds_probe(struct vio_dev
*vdev
, const struct vio_device_id
*id
)
1159 static int ds_version_printed
;
1160 struct ldc_channel_config ds_cfg
= {
1163 .mode
= LDC_MODE_STREAM
,
1165 struct mdesc_handle
*hp
;
1166 struct ldc_channel
*lp
;
1171 if (ds_version_printed
++ == 0)
1172 printk(KERN_INFO
"%s", version
);
1174 dp
= kzalloc(sizeof(*dp
), GFP_KERNEL
);
1180 val
= mdesc_get_property(hp
, vdev
->mp
, "id", NULL
);
1185 dp
->rcv_buf
= kzalloc(4096, GFP_KERNEL
);
1189 dp
->rcv_buf_len
= 4096;
1191 dp
->ds_states
= kmemdup(ds_states_template
,
1192 sizeof(ds_states_template
), GFP_KERNEL
);
1194 goto out_free_rcv_buf
;
1196 dp
->num_ds_states
= ARRAY_SIZE(ds_states_template
);
1198 for (i
= 0; i
< dp
->num_ds_states
; i
++)
1199 dp
->ds_states
[i
].handle
= ((u64
)i
<< 32);
1201 ds_cfg
.tx_irq
= vdev
->tx_irq
;
1202 ds_cfg
.rx_irq
= vdev
->rx_irq
;
1204 lp
= ldc_alloc(vdev
->channel_id
, &ds_cfg
, dp
, "DS");
1207 goto out_free_ds_states
;
1215 spin_lock_irq(&ds_lock
);
1216 dp
->next
= ds_info_list
;
1218 spin_unlock_irq(&ds_lock
);
1226 kfree(dp
->ds_states
);
1238 static int ds_remove(struct vio_dev
*vdev
)
1243 static const struct vio_device_id ds_match
[] = {
1245 .type
= "domain-services-port",
1250 static struct vio_driver ds_driver
= {
1251 .id_table
= ds_match
,
1253 .remove
= ds_remove
,
1257 static int __init
ds_init(void)
1259 unsigned long hv_ret
, major
, minor
;
1261 if (tlb_type
== hypervisor
) {
1262 hv_ret
= sun4v_get_version(HV_GRP_REBOOT_DATA
, &major
, &minor
);
1263 if (hv_ret
== HV_EOK
) {
1264 pr_info("SUN4V: Reboot data supported (maj=%lu,min=%lu).\n",
1266 reboot_data_supported
= 1;
1269 kthread_run(ds_thread
, NULL
, "kldomd");
1271 return vio_register_driver(&ds_driver
);
1274 fs_initcall(ds_init
);