4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * Copyright 2019 Joyent, Inc.
26 * Copyright 2023 Oxide Computer Company
30 * Sun NDI hotplug interfaces
34 #include <sys/sysmacros.h>
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/cmn_err.h>
40 #include <sys/debug.h>
41 #include <sys/avintr.h>
42 #include <sys/autoconf.h>
43 #include <sys/sunndi.h>
44 #include <sys/ndi_impldefs.h>
48 #include <sys/callb.h>
49 #include <sys/sysevent.h>
50 #include <sys/sysevent/eventdefs.h>
51 #include <sys/sysevent/dr.h>
52 #include <sys/taskq.h>
54 /* Local functions prototype */
55 static void ddihp_cn_run_event(void *arg
);
56 static int ddihp_cn_req_handler(ddi_hp_cn_handle_t
*hdlp
,
57 ddi_hp_cn_state_t target_state
);
60 * Global functions (called by hotplug controller or nexus drivers)
64 * Register the Hotplug Connection (CN)
67 ndi_hp_register(dev_info_t
*dip
, ddi_hp_cn_info_t
*info_p
)
69 ddi_hp_cn_handle_t
*hdlp
;
71 DDI_HP_NEXDBG((CE_CONT
, "ndi_hp_register: dip %p, info_p %p\n",
72 (void *)dip
, (void *)info_p
));
74 ASSERT(!servicing_interrupt());
75 if (servicing_interrupt())
78 /* Validate the arguments */
79 if ((dip
== NULL
) || (info_p
== NULL
))
82 if (!NEXUS_HAS_HP_OP(dip
)) {
85 /* Lock before access */
88 hdlp
= ddihp_cn_name_to_handle(dip
, info_p
->cn_name
);
90 /* This cn_name is already registered. */
96 * Create and initialize hotplug Connection handle
98 hdlp
= (ddi_hp_cn_handle_t
*)kmem_zalloc(
99 (sizeof (ddi_hp_cn_handle_t
)), KM_SLEEP
);
101 /* Copy the Connection information */
103 bcopy(info_p
, &(hdlp
->cn_info
), sizeof (*info_p
));
106 hdlp
->cn_info
.cn_name
= ddi_strdup(info_p
->cn_name
, KM_SLEEP
);
108 if (ddihp_cn_getstate(hdlp
) != DDI_SUCCESS
) {
109 DDI_HP_NEXDBG((CE_CONT
, "ndi_hp_register: dip %p, hdlp %p"
110 "ddi_cn_getstate failed\n", (void *)dip
, (void *)hdlp
));
116 * Append the handle to the list
118 DDIHP_LIST_APPEND(ddi_hp_cn_handle_t
, (DEVI(dip
)->devi_hp_hdlp
),
123 return (NDI_SUCCESS
);
126 kmem_free(hdlp
->cn_info
.cn_name
, strlen(hdlp
->cn_info
.cn_name
) + 1);
127 kmem_free(hdlp
, sizeof (ddi_hp_cn_handle_t
));
130 return (NDI_FAILURE
);
134 * Unregister a Hotplug Connection (CN)
137 ndi_hp_unregister(dev_info_t
*dip
, char *cn_name
)
139 ddi_hp_cn_handle_t
*hdlp
;
142 DDI_HP_NEXDBG((CE_CONT
, "ndi_hp_unregister: dip %p, cn name %s\n",
143 (void *)dip
, cn_name
));
145 ASSERT(!servicing_interrupt());
146 if (servicing_interrupt())
147 return (NDI_FAILURE
);
149 /* Validate the arguments */
150 if ((dip
== NULL
) || (cn_name
== NULL
))
155 hdlp
= ddihp_cn_name_to_handle(dip
, cn_name
);
161 switch (ddihp_cn_unregister(hdlp
)) {
182 * Notify the Hotplug Connection (CN) to change state.
184 * DDI_HP_REQ_SYNC Return after the change is finished.
185 * DDI_HP_REQ_ASYNC Return after the request is dispatched.
188 ndi_hp_state_change_req(dev_info_t
*dip
, char *cn_name
,
189 ddi_hp_cn_state_t state
, uint_t flag
)
191 ddi_hp_cn_async_event_entry_t
*eventp
;
193 DDI_HP_NEXDBG((CE_CONT
, "ndi_hp_state_change_req: dip %p "
194 "cn_name: %s, state %x, flag %x\n",
195 (void *)dip
, cn_name
, state
, flag
));
197 /* Validate the arguments */
198 if (dip
== NULL
|| cn_name
== NULL
)
201 if (!NEXUS_HAS_HP_OP(dip
)) {
202 return (NDI_ENOTSUP
);
205 * If the request is to handle the event synchronously, then call
206 * the event handler without queuing the event.
208 if (flag
& DDI_HP_REQ_SYNC
) {
209 ddi_hp_cn_handle_t
*hdlp
;
212 ASSERT(!servicing_interrupt());
213 if (servicing_interrupt())
214 return (NDI_FAILURE
);
218 hdlp
= ddihp_cn_name_to_handle(dip
, cn_name
);
225 DDI_HP_NEXDBG((CE_CONT
, "ndi_hp_state_change_req: hdlp %p "
226 "calling ddihp_cn_req_handler() directly to handle "
227 "target_state %x\n", (void *)hdlp
, state
));
229 ret
= ddihp_cn_req_handler(hdlp
, state
);
236 eventp
= kmem_zalloc(sizeof (ddi_hp_cn_async_event_entry_t
),
241 eventp
->cn_name
= ddi_strdup(cn_name
, KM_NOSLEEP
);
242 if (eventp
->cn_name
== NULL
) {
243 kmem_free(eventp
, sizeof (ddi_hp_cn_async_event_entry_t
));
247 eventp
->target_state
= state
;
250 * Hold the parent's ref so that it won't disappear when the taskq is
255 if (taskq_dispatch(system_taskq
, ddihp_cn_run_event
, eventp
,
256 TQ_NOSLEEP
) == TASKQID_INVALID
) {
258 DDI_HP_NEXDBG((CE_CONT
, "ndi_hp_state_change_req: "
259 "taskq_dispatch failed! dip %p "
260 "target_state %x\n", (void *)dip
, state
));
264 return (NDI_CLAIMED
);
268 * Walk the link of Hotplug Connection handles of a dip:
269 * DEVI(dip)->devi_hp_hdlp->[link of connections]
272 ndi_hp_walk_cn(dev_info_t
*dip
, int (*f
)(ddi_hp_cn_info_t
*,
275 ddi_hp_cn_handle_t
*head
, *curr
, *prev
;
277 DDI_HP_NEXDBG((CE_CONT
, "ndi_hp_walk_cn: dip %p arg %p\n",
280 ASSERT(!servicing_interrupt());
281 if (servicing_interrupt())
284 /* Validate the arguments */
290 head
= DEVI(dip
)->devi_hp_hdlp
;
293 while (curr
!= NULL
) {
294 DDI_HP_NEXDBG((CE_CONT
, "ndi_hp_walk_cn: dip %p "
295 "current cn_name: %s\n",
296 (void *)dip
, curr
->cn_info
.cn_name
));
297 switch ((*f
)(&(curr
->cn_info
), arg
)) {
298 case DDI_WALK_TERMINATE
:
302 case DDI_WALK_CONTINUE
:
304 if (DEVI(dip
)->devi_hp_hdlp
!= head
) {
306 * The current node is head and it is removed
307 * by last call to (*f)()
309 head
= DEVI(dip
)->devi_hp_hdlp
;
312 } else if (prev
&& prev
->next
!= curr
) {
314 * The current node is a middle node or tail
315 * node and it is removed by last call to
320 /* no removal accurred on curr node */
330 * Local functions (called within this file)
334 * Wrapper function for ddihp_cn_req_handler() called from taskq
337 ddihp_cn_run_event(void *arg
)
339 ddi_hp_cn_async_event_entry_t
*eventp
=
340 (ddi_hp_cn_async_event_entry_t
*)arg
;
341 dev_info_t
*dip
= eventp
->dip
;
343 ddi_hp_cn_handle_t
*hdlp
;
346 * See notes in ddihp_modctl(). This is another essentially identical
347 * path we get to internally rather than from userland, but the same
348 * problem applies here.
350 pdip
= ddi_get_parent(dip
);
352 ndi_devi_enter(pdip
);
354 /* Lock before access */
357 hdlp
= ddihp_cn_name_to_handle(dip
, eventp
->cn_name
);
359 (void) ddihp_cn_req_handler(hdlp
, eventp
->target_state
);
361 DDI_HP_NEXDBG((CE_CONT
, "ddihp_cn_run_event: no handle for "
362 "cn_name: %s dip %p. Request for target_state %x is"
364 eventp
->cn_name
, (void *)dip
, eventp
->target_state
));
371 /* Release the devi's ref that is held from interrupt context. */
372 ndi_rele_devi((dev_info_t
*)DEVI(dip
));
373 kmem_free(eventp
->cn_name
, strlen(eventp
->cn_name
) + 1);
374 kmem_free(eventp
, sizeof (ddi_hp_cn_async_event_entry_t
));
378 * Handle state change request of a Hotplug Connection (CN)
381 ddihp_cn_req_handler(ddi_hp_cn_handle_t
*hdlp
,
382 ddi_hp_cn_state_t target_state
)
384 dev_info_t
*dip
= hdlp
->cn_dip
;
385 int ret
= DDI_SUCCESS
;
387 DDI_HP_NEXDBG((CE_CONT
, "ddihp_cn_req_handler:"
388 " hdlp %p, target_state %x\n",
389 (void *)hdlp
, target_state
));
391 ASSERT(DEVI_BUSY_OWNED(dip
));
394 * We do not want to fetch the state first, as calling ddihp_cn_getstate
395 * will update the cn_state member of the connection handle. The
396 * connector's hotplug operations rely on this value to know how
397 * target_state compares to the last known state of the device and make
398 * decisions about whether to clean up, post sysevents about the state
401 * Instead, just carry out the request to change the state. The
402 * connector's hotplug operations will update the state in the
403 * connection handle after they complete their necessary state change
406 if (hdlp
->cn_info
.cn_state
!= target_state
) {
407 ddi_hp_cn_state_t result_state
= 0;
409 DDIHP_CN_OPS(hdlp
, DDI_HPOP_CN_CHANGE_STATE
,
410 (void *)&target_state
, (void *)&result_state
, ret
);
412 DDI_HP_NEXDBG((CE_CONT
, "ddihp_cn_req_handler: dip %p, "
413 "hdlp %p changed state to %x, ret=%x\n",
414 (void *)dip
, (void *)hdlp
, result_state
, ret
));
417 if (ret
== DDI_SUCCESS
)
418 return (NDI_CLAIMED
);
420 return (NDI_UNCLAIMED
);