17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27*d75f3745SJohn Levon /*
28*d75f3745SJohn Levon  * Copyright (c) 2018, Joyent, Inc.
29*d75f3745SJohn Levon  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include "ndievents.h"
327c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
337c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
347c478bd9Sstevel@tonic-gate #include <sys/dditypes.h>
357c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
367c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
377c478bd9Sstevel@tonic-gate #include <sys/param.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate int
dip_to_pathname(struct dev_info * device,char * path,int buflen)41*d75f3745SJohn Levon dip_to_pathname(struct dev_info *device, char *path, int buflen)
42*d75f3745SJohn Levon {
437c478bd9Sstevel@tonic-gate 	char *bp;
447c478bd9Sstevel@tonic-gate 	char *addr;
457c478bd9Sstevel@tonic-gate 	char addr_str[32];
467c478bd9Sstevel@tonic-gate 	char nodename[MAXNAMELEN];
477c478bd9Sstevel@tonic-gate 	struct dev_info devi_parent;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 	if (!device) {
507c478bd9Sstevel@tonic-gate 		mdb_warn("Unable to access devinfo.");
517c478bd9Sstevel@tonic-gate 		return (-1);
527c478bd9Sstevel@tonic-gate 	}
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	if (device->devi_parent == NULL) {
557c478bd9Sstevel@tonic-gate 		if (mdb_readstr(nodename, sizeof (nodename),
567c478bd9Sstevel@tonic-gate 		    (uintptr_t)device->devi_node_name) == -1) {
57*d75f3745SJohn Levon 			return (-1);
587c478bd9Sstevel@tonic-gate 		}
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 		if (sizeof (nodename) > (buflen - strlen(path))) {
617c478bd9Sstevel@tonic-gate 			return (-1);
627c478bd9Sstevel@tonic-gate 		}
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 		strncpy(path, nodename, sizeof (nodename));
657c478bd9Sstevel@tonic-gate 		return (0);
667c478bd9Sstevel@tonic-gate 	}
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	if (mdb_vread(&devi_parent, sizeof (struct dev_info),
697c478bd9Sstevel@tonic-gate 	    (uintptr_t)device->devi_parent) == -1) {
707c478bd9Sstevel@tonic-gate 		mdb_warn("Unable to access devi_parent at %p",
717c478bd9Sstevel@tonic-gate 		    (uintptr_t)device->devi_parent);
727c478bd9Sstevel@tonic-gate 		return (-1);
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	if (dip_to_pathname(&devi_parent, path, buflen) == -1) {
767c478bd9Sstevel@tonic-gate 		return (-1);
777c478bd9Sstevel@tonic-gate 	}
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	if (mdb_readstr(nodename, sizeof (nodename),
807c478bd9Sstevel@tonic-gate 	    (uintptr_t)device->devi_node_name) == -1) {
817c478bd9Sstevel@tonic-gate 		return (-1);
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if (device->devi_node_state < DS_INITIALIZED) {
85*d75f3745SJohn Levon 		addr_str[0] = '\0';
867c478bd9Sstevel@tonic-gate 	} else {
877c478bd9Sstevel@tonic-gate 		addr = device->devi_addr;
887c478bd9Sstevel@tonic-gate 		if (mdb_readstr(addr_str, sizeof (addr_str),
897c478bd9Sstevel@tonic-gate 		    (uintptr_t)addr) == -1) {
907c478bd9Sstevel@tonic-gate 			return (-1);
917c478bd9Sstevel@tonic-gate 		}
927c478bd9Sstevel@tonic-gate 	}
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	bp = path + strlen(path);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	if (addr_str[0] == '\0') {
977c478bd9Sstevel@tonic-gate 		(void) mdb_snprintf(bp, buflen - strlen(path), "/%s", nodename);
987c478bd9Sstevel@tonic-gate 	} else {
997c478bd9Sstevel@tonic-gate 		(void) mdb_snprintf(bp, buflen - strlen(path), "/%s@%s",
1007c478bd9Sstevel@tonic-gate 		    nodename, addr_str);
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 	return (0);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1077c478bd9Sstevel@tonic-gate int
ndi_callback_print(struct ndi_event_cookie * cookie,uint_t flags)1087c478bd9Sstevel@tonic-gate ndi_callback_print(struct ndi_event_cookie *cookie, uint_t flags)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	struct ndi_event_callbacks *callback_list;
1127c478bd9Sstevel@tonic-gate 	struct ndi_event_callbacks cb;
1137c478bd9Sstevel@tonic-gate 	char device_path[MAXPATHLEN];
1147c478bd9Sstevel@tonic-gate 	struct dev_info devi;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	if (!cookie) {
1177c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	callback_list = cookie->callback_list;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	while (callback_list != NULL) {
1237c478bd9Sstevel@tonic-gate 		if (mdb_vread(&cb, sizeof (struct ndi_event_callbacks),
124*d75f3745SJohn Levon 		    (uintptr_t)callback_list) == -1) {
1257c478bd9Sstevel@tonic-gate 			mdb_warn("Could not read callback structure at"
1267c478bd9Sstevel@tonic-gate 			    " %p", callback_list);
1277c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
1287c478bd9Sstevel@tonic-gate 		}
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 		if (mdb_vread(&devi, sizeof (struct dev_info),
1317c478bd9Sstevel@tonic-gate 		    (uintptr_t)cb.ndi_evtcb_dip) == -1) {
1327c478bd9Sstevel@tonic-gate 			mdb_warn("Could not read devinfo structure at"
1337c478bd9Sstevel@tonic-gate 			    " %p", cb.ndi_evtcb_dip);
1347c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 		if (dip_to_pathname(&devi, device_path, sizeof (device_path))
1387c478bd9Sstevel@tonic-gate 		    == -1) {
1397c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
1407c478bd9Sstevel@tonic-gate 		}
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 		mdb_printf("\t\tCallback Registered By: %s\n", device_path);
1437c478bd9Sstevel@tonic-gate 		mdb_printf("\t\t  Callback Address:\t%-?p\n"
1447c478bd9Sstevel@tonic-gate 		    "\t\t  Callback Function:\t%-p\n"
1457c478bd9Sstevel@tonic-gate 		    "\t\t  Callback Args:\t%-?p\n"
1467c478bd9Sstevel@tonic-gate 		    "\t\t  Callback Cookie:\t%-?p\n",
1477c478bd9Sstevel@tonic-gate 		    callback_list, cb.ndi_evtcb_callback, cb.ndi_evtcb_arg,
1487c478bd9Sstevel@tonic-gate 		    cb.ndi_evtcb_cookie);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 		callback_list = cb.ndi_evtcb_next;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	return (DCMD_OK);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate int
ndi_event_print(struct ndi_event_hdl * hdl,uint_t flags)1587c478bd9Sstevel@tonic-gate ndi_event_print(struct ndi_event_hdl *hdl, uint_t flags)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 
161*d75f3745SJohn Levon 	struct	ndi_event_definition def;
162*d75f3745SJohn Levon 	struct	ndi_event_cookie cookie;
163*d75f3745SJohn Levon 	struct	ndi_event_cookie *cookie_list;
164*d75f3745SJohn Levon 	char	ndi_event_name[256];
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	if (!hdl)
1677c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	cookie_list = hdl->ndi_evthdl_cookie_list;
1707c478bd9Sstevel@tonic-gate 	if (cookie_list == NULL) {
1717c478bd9Sstevel@tonic-gate 		mdb_printf("\tNo cookies defined for this handle.\n");
1727c478bd9Sstevel@tonic-gate 		return (DCMD_OK);
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	while (cookie_list != NULL) {
1767c478bd9Sstevel@tonic-gate 		if (mdb_vread(&cookie, sizeof (struct ndi_event_cookie),
1777c478bd9Sstevel@tonic-gate 		    (uintptr_t)cookie_list) == -1) {
1787c478bd9Sstevel@tonic-gate 			mdb_warn("Unable to access cookie list");
1797c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
1807c478bd9Sstevel@tonic-gate 		}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 		if (mdb_vread(&def, sizeof (struct ndi_event_definition),
1837c478bd9Sstevel@tonic-gate 		    (uintptr_t)cookie.definition) == -1) {
1847c478bd9Sstevel@tonic-gate 			mdb_warn("Unable to access definition at %p",
1857c478bd9Sstevel@tonic-gate 			    cookie.definition);
1867c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
1877c478bd9Sstevel@tonic-gate 		}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 		if (mdb_readstr(ndi_event_name, sizeof (ndi_event_name),
1907c478bd9Sstevel@tonic-gate 		    (uintptr_t)def.ndi_event_name) == -1) {
1917c478bd9Sstevel@tonic-gate 			mdb_warn("Unable to read cookie name.");
1927c478bd9Sstevel@tonic-gate 			return (DCMD_ERR);
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 		mdb_printf("\tCookie(%s %p) :Plevel(%d)\n\tddip(%p)"
1967c478bd9Sstevel@tonic-gate 		    " : Attr(%d)\n",
1977c478bd9Sstevel@tonic-gate 		    ndi_event_name, cookie_list, def.ndi_event_plevel,
1987c478bd9Sstevel@tonic-gate 		    cookie.ddip, def.ndi_event_attributes);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 		ndi_callback_print(&cookie, flags);
2017c478bd9Sstevel@tonic-gate 		cookie_list = cookie.next_cookie;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 	return (0);
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2087c478bd9Sstevel@tonic-gate int
ndi_event_hdl(uintptr_t addr,uint_t flags,int argc,const mdb_arg_t * argv)2097c478bd9Sstevel@tonic-gate ndi_event_hdl(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	struct dev_info devi;
2137c478bd9Sstevel@tonic-gate 	struct ndi_event_hdl handle;
2147c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
2157c478bd9Sstevel@tonic-gate 	int done;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	if (!(flags & DCMD_ADDRSPEC)) {
2187c478bd9Sstevel@tonic-gate 		return (DCMD_USAGE);
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	if (mdb_vread(&handle, sizeof (struct ndi_event_hdl), addr) == -1) {
2227c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read ndi_event_hdl at %p", addr);
2237c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if (mdb_vread(&devi, sizeof (struct dev_info),
227*d75f3745SJohn Levon 	    (uintptr_t)handle.ndi_evthdl_dip) == -1) {
2287c478bd9Sstevel@tonic-gate 		mdb_warn("failed to read devinfo node at %p",
2297c478bd9Sstevel@tonic-gate 		    handle.ndi_evthdl_dip);
2307c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	if (dip_to_pathname(&devi, path, sizeof (path)) == -1) {
2347c478bd9Sstevel@tonic-gate 		return (DCMD_ERR);
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	done = 0;
2387c478bd9Sstevel@tonic-gate 	while (!done) {
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 		mdb_printf("%<b>Handle%</b> (%p) :%<b> Path%</b> (%s) : %<b>"
2417c478bd9Sstevel@tonic-gate 		    "dip %</b>(%p) \n", addr, path, handle.ndi_evthdl_dip);
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 		mdb_printf("mutexes:	handle(%p)	callback(%p)\n",
2447c478bd9Sstevel@tonic-gate 		    handle.ndi_evthdl_mutex, handle.ndi_evthdl_cb_mutex);
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 		ndi_event_print(&handle, flags);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 		if (handle.ndi_next_hdl == NULL) {
2497c478bd9Sstevel@tonic-gate 			done = 1;
2507c478bd9Sstevel@tonic-gate 		} else {
2517c478bd9Sstevel@tonic-gate 			addr = (uintptr_t)handle.ndi_next_hdl;
2527c478bd9Sstevel@tonic-gate 			if (mdb_vread(&handle, sizeof (struct ndi_event_hdl),
2537c478bd9Sstevel@tonic-gate 			    (uintptr_t)addr) == -1) {
254*d75f3745SJohn Levon 				mdb_warn("failed to read ndi_event_hdl at %p",
255*d75f3745SJohn Levon 				    addr);
256*d75f3745SJohn Levon 				break;
2577c478bd9Sstevel@tonic-gate 			}
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 		}
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	return (0);
2637c478bd9Sstevel@tonic-gate }
264