xref: /illumos-gate/usr/src/cmd/mdb/intel/modules/sata/sata.c (revision 50d967713af8725306d090dd29033d9efe924715)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
27  * Copyright 2019 Joyent, Inc.
28  */
29 
30 #include <sys/mdb_modapi.h>
31 #include <mdb/mdb_ks.h>
32 #include <sys/modctl.h>
33 #include <note.h>
34 #include <sys/ddi_impldefs.h>
35 #include <sys/ddidmareq.h>
36 #include <sys/devops.h>
37 #include <time.h>
38 #include <sys/varargs.h>
39 #include <sys/sata/sata_hba.h>
40 
41 /*
42  * SATA trace debug walker/dcmd code
43  */
44 
45 /*
46  * Initialize the sata_trace_dmsg_t walker by either using the given starting
47  * address, or reading the value of the kernel's sata_debug_rbuf pointer.
48  * We also allocate a sata_trace_dmsg_t for storage, and save this using the
49  * walk_data pointer.
50  */
51 static int
52 sata_dmsg_walk_i(mdb_walk_state_t *wsp)
53 {
54 	uintptr_t rbuf_addr;
55 	sata_trace_rbuf_t rbuf;
56 
57 	if (wsp->walk_addr == 0) {
58 		if (mdb_readvar(&rbuf_addr, "sata_debug_rbuf") == -1) {
59 			mdb_warn("failed to read 'sata_debug_rbuf'");
60 			return (WALK_ERR);
61 		}
62 
63 		if (mdb_vread(&rbuf, sizeof (sata_trace_rbuf_t), rbuf_addr)
64 		    == -1) {
65 			mdb_warn("failed to read sata_trace_rbuf_t at %p",
66 			    rbuf_addr);
67 			return (WALK_ERR);
68 		}
69 
70 		wsp->walk_addr = (uintptr_t)(sata_trace_dmsg_t *)rbuf.dmsgh;
71 	}
72 
73 	/*
74 	 * Save ptr to head of ring buffer to prevent looping.
75 	 */
76 	wsp->walk_arg = (void *)wsp->walk_addr;
77 	wsp->walk_data = mdb_alloc(sizeof (sata_trace_dmsg_t), UM_SLEEP);
78 	return (WALK_NEXT);
79 }
80 
81 /*
82  * At each step, read a sata_trace_dmsg_t into our private storage, and then
83  * invoke the callback function.  We terminate when we reach a NULL next
84  * pointer.
85  */
86 static int
87 sata_dmsg_walk_s(mdb_walk_state_t *wsp)
88 {
89 	int status;
90 
91 	if (wsp->walk_addr == 0)
92 		return (WALK_DONE);
93 
94 	if (mdb_vread(wsp->walk_data, sizeof (sata_trace_dmsg_t),
95 	    wsp->walk_addr) == -1) {
96 		mdb_warn("failed to read sata_trace_dmsg_t at %p",
97 		    wsp->walk_addr);
98 		return (WALK_ERR);
99 	}
100 
101 	status = wsp->walk_callback(wsp->walk_addr, wsp->walk_data,
102 	    wsp->walk_cbdata);
103 
104 	wsp->walk_addr =
105 	    (uintptr_t)(((sata_trace_dmsg_t *)wsp->walk_data)->next);
106 
107 	/*
108 	 * If we've looped then we're done.
109 	 */
110 	if (wsp->walk_addr == (uintptr_t)wsp->walk_arg)
111 		wsp->walk_addr = 0;
112 
113 	return (status);
114 }
115 
116 /*
117  * The walker's fini function is invoked at the end of each walk.  Since we
118  * dynamically allocated a sata_trace_dmsg_t in sata_dmsg_walk_i, we must
119  * free it now.
120  */
121 static void
122 sata_dmsg_walk_f(mdb_walk_state_t *wsp)
123 {
124 	mdb_free(wsp->walk_data, sizeof (sata_trace_dmsg_t));
125 }
126 
127 /*
128  * This routine is used by the sata_dmsg_dump dcmd to dump content of
129  * SATA trace ring buffer.
130  */
131 int
132 sata_dmsg_dump(sata_trace_dmsg_t *addr, int print_pathname, uint_t *printed)
133 {
134 	sata_trace_dmsg_t	dmsg, *dmsgh = addr;
135 	struct dev_info		dev;
136 	char			drivername[MODMAXNAMELEN];
137 	char			pathname[MAXPATHLEN];
138 	char			merge[1024];
139 
140 	while (addr != NULL) {
141 		if (mdb_vread(&dmsg, sizeof (dmsg), (uintptr_t)addr) !=
142 		    sizeof (dmsg)) {
143 			mdb_warn("failed to read message pointer in kernel");
144 			return (DCMD_ERR);
145 		}
146 
147 		if (dmsg.dip != NULL) {
148 			if ((mdb_vread(&dev, sizeof (struct dev_info),
149 			    (uintptr_t)dmsg.dip)) == -1) {
150 				(void) mdb_snprintf(merge, sizeof (merge),
151 				    "[%Y:%03d:%03d:%03d] : %s",
152 				    dmsg.timestamp.tv_sec,
153 				    (int)dmsg.timestamp.tv_nsec/1000000,
154 				    (int)(dmsg.timestamp.tv_nsec/1000)%1000,
155 				    (int)dmsg.timestamp.tv_nsec%1000,
156 				    dmsg.buf);
157 			} else {
158 				(void) mdb_devinfo2driver((uintptr_t)dmsg.dip,
159 				    drivername, sizeof (drivername));
160 				(void) mdb_snprintf(merge, sizeof (merge),
161 				    "[%Y:%03d:%03d:%03d] %s%d: %s",
162 				    dmsg.timestamp.tv_sec,
163 				    (int)dmsg.timestamp.tv_nsec/1000000,
164 				    (int)(dmsg.timestamp.tv_nsec/1000)%1000,
165 				    (int)dmsg.timestamp.tv_nsec%1000,
166 				    drivername,
167 				    dev.devi_instance,
168 				    dmsg.buf);
169 
170 				if (print_pathname == TRUE) {
171 					(void) mdb_ddi_pathname(
172 					    (uintptr_t)dmsg.dip, pathname,
173 					    sizeof (pathname));
174 					mdb_printf("[%s]", pathname);
175 				}
176 			}
177 		} else {
178 			(void) mdb_snprintf(merge, sizeof (merge),
179 			    "[%Y:%03d:%03d:%03d] : %s",
180 			    dmsg.timestamp.tv_sec,
181 			    (int)dmsg.timestamp.tv_nsec/1000000,
182 			    (int)(dmsg.timestamp.tv_nsec/1000)%1000,
183 			    (int)dmsg.timestamp.tv_nsec%1000,
184 			    dmsg.buf);
185 		}
186 
187 		mdb_printf("%s\n", merge);
188 
189 		if (printed != NULL) {
190 			(*printed)++;
191 		}
192 
193 		if (((addr = dmsg.next) == NULL) || (dmsg.next == dmsgh)) {
194 			break;
195 		}
196 	}
197 
198 	return (DCMD_OK);
199 }
200 
201 /*
202  * 1. Process flag passed to sata_dmsg_dump dcmd.
203  * 2. Obtain SATA trace ring buffer pointer.
204  * 3. Pass SATA trace ring buffer pointer to sata_dmsg_dump()
205  *    to dump content of SATA trace ring buffer.
206  */
207 int
208 sata_rbuf_dump(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
209 {
210 	sata_trace_rbuf_t	rbuf;
211 	uint_t		printed = 0; /* have we printed anything? */
212 	int		print_pathname = FALSE;
213 	int		rval = DCMD_OK;
214 
215 	if (argc > 1) {
216 		return (DCMD_USAGE);
217 	}
218 
219 	if (mdb_getopts(argc, argv,
220 	    'a', MDB_OPT_SETBITS, TRUE, &print_pathname, NULL) != argc) {
221 		return (DCMD_USAGE);
222 	}
223 
224 	/*
225 	 * If ring buffer address not provided try to obtain
226 	 * it using sata_debug_rbuf global.
227 	 */
228 	if ((addr == 0) || !(flags & DCMD_ADDRSPEC)) {
229 		if (mdb_readvar(&addr, "sata_debug_rbuf") == -1) {
230 			mdb_warn("Failed to read 'sata_debug_rbuf'.");
231 			return (DCMD_ERR);
232 		}
233 	}
234 
235 	if (mdb_vread(&rbuf, sizeof (rbuf), addr) != sizeof (rbuf)) {
236 		mdb_warn("Failed to read ring buffer in kernel.");
237 		return (DCMD_ERR);
238 	}
239 
240 	if (rbuf.dmsgh == NULL) {
241 		mdb_printf("The sata trace ring buffer is empty.\n");
242 		return (DCMD_OK);
243 	}
244 
245 	rval = sata_dmsg_dump((sata_trace_dmsg_t *)rbuf.dmsgh,
246 	    print_pathname, &printed);
247 
248 	if (rval != DCMD_OK) {
249 		return (rval);
250 	}
251 
252 	if (printed == 0) {
253 		mdb_warn("Failed to read sata trace ring buffer.");
254 		return (DCMD_ERR);
255 	}
256 
257 	return (rval);
258 }
259 
260 /*
261  * MDB module linkage information:
262  *
263  * We declare a list of structures describing our dcmds, a list of structures
264  * describing our walkers, and a function named _mdb_init to return a pointer
265  * to our module information.
266  */
267 
268 static const mdb_dcmd_t dcmds[] = {
269 	{ "sata_dmsg_dump", "[-a]", "Dump sata trace debug messages",
270 	    sata_rbuf_dump },
271 	{ NULL }
272 };
273 
274 static const mdb_walker_t walkers[] = {
275 	{ "sata_dmsg",
276 	    "walk ring buffer containing sata trace debug messages",
277 	    sata_dmsg_walk_i, sata_dmsg_walk_s, sata_dmsg_walk_f },
278 	{ NULL }
279 };
280 
281 static const mdb_modinfo_t modinfo = {
282 	MDB_API_VERSION, dcmds, walkers
283 };
284 
285 const mdb_modinfo_t *
286 _mdb_init(void)
287 {
288 	return (&modinfo);
289 }
290