15942 PCIe hotplug insertion does not configure the slot
[illumos-gate.git] / usr / src / uts / common / os / ddi_hp_ndi.c
blob137984dd880b047d8b917eb0922587ed7531cbea
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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
33 #include <sys/note.h>
34 #include <sys/sysmacros.h>
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kmem.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>
45 #include <sys/ddi.h>
46 #include <sys/disp.h>
47 #include <sys/stat.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)
66 int
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())
76 return (NDI_FAILURE);
78 /* Validate the arguments */
79 if ((dip == NULL) || (info_p == NULL))
80 return (NDI_EINVAL);
82 if (!NEXUS_HAS_HP_OP(dip)) {
83 return (NDI_ENOTSUP);
85 /* Lock before access */
86 ndi_devi_enter(dip);
88 hdlp = ddihp_cn_name_to_handle(dip, info_p->cn_name);
89 if (hdlp) {
90 /* This cn_name is already registered. */
91 ndi_devi_exit(dip);
93 return (NDI_SUCCESS);
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 */
102 hdlp->cn_dip = dip;
103 bcopy(info_p, &(hdlp->cn_info), sizeof (*info_p));
105 /* Copy cn_name */
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));
112 goto fail;
116 * Append the handle to the list
118 DDIHP_LIST_APPEND(ddi_hp_cn_handle_t, (DEVI(dip)->devi_hp_hdlp),
119 hdlp);
121 ndi_devi_exit(dip);
123 return (NDI_SUCCESS);
125 fail:
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));
128 ndi_devi_exit(dip);
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;
140 int ret;
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))
151 return (NDI_EINVAL);
153 ndi_devi_enter(dip);
155 hdlp = ddihp_cn_name_to_handle(dip, cn_name);
156 if (hdlp == NULL) {
157 ndi_devi_exit(dip);
158 return (NDI_EINVAL);
161 switch (ddihp_cn_unregister(hdlp)) {
162 case DDI_SUCCESS:
163 ret = NDI_SUCCESS;
164 break;
165 case DDI_EINVAL:
166 ret = NDI_EINVAL;
167 break;
168 case DDI_EBUSY:
169 ret = NDI_BUSY;
170 break;
171 default:
172 ret = NDI_FAILURE;
173 break;
176 ndi_devi_exit(dip);
178 return (ret);
182 * Notify the Hotplug Connection (CN) to change state.
183 * Flag:
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)
199 return (NDI_EINVAL);
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;
210 int ret;
212 ASSERT(!servicing_interrupt());
213 if (servicing_interrupt())
214 return (NDI_FAILURE);
216 ndi_devi_enter(dip);
218 hdlp = ddihp_cn_name_to_handle(dip, cn_name);
219 if (hdlp == NULL) {
220 ndi_devi_exit(dip);
222 return (NDI_EINVAL);
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);
231 ndi_devi_exit(dip);
233 return (ret);
236 eventp = kmem_zalloc(sizeof (ddi_hp_cn_async_event_entry_t),
237 KM_NOSLEEP);
238 if (eventp == NULL)
239 return (NDI_NOMEM);
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));
244 return (NDI_NOMEM);
246 eventp->dip = dip;
247 eventp->target_state = state;
250 * Hold the parent's ref so that it won't disappear when the taskq is
251 * scheduled to run.
253 ndi_hold_devi(dip);
255 if (taskq_dispatch(system_taskq, ddihp_cn_run_event, eventp,
256 TQ_NOSLEEP) == TASKQID_INVALID) {
257 ndi_rele_devi(dip);
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));
261 return (NDI_NOMEM);
264 return (NDI_CLAIMED);
268 * Walk the link of Hotplug Connection handles of a dip:
269 * DEVI(dip)->devi_hp_hdlp->[link of connections]
271 void
272 ndi_hp_walk_cn(dev_info_t *dip, int (*f)(ddi_hp_cn_info_t *,
273 void *), void *arg)
275 ddi_hp_cn_handle_t *head, *curr, *prev;
277 DDI_HP_NEXDBG((CE_CONT, "ndi_hp_walk_cn: dip %p arg %p\n",
278 (void *)dip, arg));
280 ASSERT(!servicing_interrupt());
281 if (servicing_interrupt())
282 return;
284 /* Validate the arguments */
285 if (dip == NULL)
286 return;
288 ndi_devi_enter(dip);
290 head = DEVI(dip)->devi_hp_hdlp;
291 curr = head;
292 prev = NULL;
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:
299 ndi_devi_exit(dip);
301 return;
302 case DDI_WALK_CONTINUE:
303 default:
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;
310 curr = head;
311 prev = NULL;
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
316 * (*f)()
318 curr = prev->next;
319 } else {
320 /* no removal accurred on curr node */
321 prev = curr;
322 curr = curr->next;
326 ndi_devi_exit(dip);
330 * Local functions (called within this file)
334 * Wrapper function for ddihp_cn_req_handler() called from taskq
336 static void
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;
342 dev_info_t *pdip;
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);
351 if (pdip != NULL)
352 ndi_devi_enter(pdip);
354 /* Lock before access */
355 ndi_devi_enter(dip);
357 hdlp = ddihp_cn_name_to_handle(dip, eventp->cn_name);
358 if (hdlp) {
359 (void) ddihp_cn_req_handler(hdlp, eventp->target_state);
360 } else {
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"
363 " dropped. \n",
364 eventp->cn_name, (void *)dip, eventp->target_state));
367 ndi_devi_exit(dip);
368 if (pdip != NULL)
369 ndi_devi_exit(pdip);
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)
380 static int
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
399 * change, and so on.
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
404 * actions.
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);
419 else
420 return (NDI_UNCLAIMED);