1 /*- 2 * Copyright (c) 2018 Netflix, Inc. 3 * Written by: Scott Long <scottl@freebsd.org> 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the author nor the names of any co-contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/param.h> 31 #include <sys/errno.h> 32 #include <sys/types.h> 33 #include <sys/sysctl.h> 34 #include <err.h> 35 #include <libutil.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <unistd.h> 40 #include "mpsutil.h" 41 42 MPS_TABLE(top, debug); 43 44 struct mps_dumpreq_hdr { 45 uint32_t smid; 46 uint32_t state; 47 uint32_t numframes; 48 uint32_t deschi; 49 uint32_t desclo; 50 }; 51 52 static int find_sgl(char *); 53 static void print_sgl(char *, int, int); 54 55 #define MPS_FRAME_LEN 128 56 57 static int 58 debug_dumpreqs(int ac, char **av) 59 { 60 struct mps_dumpreq_hdr *hdr; 61 char *buf, sysctlbuf[128]; 62 size_t len; 63 int numframes, error, offset; 64 65 len = 0; 66 buf = NULL; 67 snprintf(sysctlbuf, sizeof(sysctlbuf), "dev.%s.%d.dump_reqs", 68 is_mps ? "mps" : "mpr", mps_unit); 69 70 error = sysctlbyname(sysctlbuf, NULL, &len, NULL, 0); 71 if (error) 72 return (error); 73 74 if (len == 0) 75 return (0); 76 77 buf = malloc(len); 78 if (buf == NULL) 79 return (ENOMEM); 80 81 error = sysctlbyname(sysctlbuf, buf, &len, NULL, 0); 82 if (error) { 83 printf("len= %zd, error= %d errno= %d\n", len, error, errno); 84 return (error); 85 } 86 87 while (len >= MPS_FRAME_LEN) { 88 hdr = (struct mps_dumpreq_hdr *)buf; 89 numframes = hdr->numframes; 90 91 printf("SMID= %d state= %#x numframes= %d desc.hi= %#08x " 92 "desc.lo= %#08x\n", hdr->smid, hdr->state, 93 hdr->numframes, hdr->deschi, hdr->desclo); 94 95 buf += sizeof(struct mps_dumpreq_hdr); 96 len -= sizeof(struct mps_dumpreq_hdr); 97 98 if ((offset = find_sgl(buf)) != -1) 99 print_sgl(buf, offset, numframes); 100 101 buf += MPS_FRAME_LEN * numframes; 102 len -= MPS_FRAME_LEN * numframes; 103 } 104 105 return (error); 106 } 107 108 static int 109 find_sgl(char *buf) 110 { 111 MPI2_REQUEST_HEADER *req; 112 MPI2_SCSI_IO_REQUEST *scsi; 113 int offset = 0; 114 115 req = (MPI2_REQUEST_HEADER *)buf; 116 117 switch (req->Function) { 118 case MPI2_FUNCTION_SCSI_IO_REQUEST: 119 scsi = (MPI2_SCSI_IO_REQUEST *)buf; 120 offset = scsi->SGLOffset0; 121 break; 122 default: 123 offset = -1; 124 } 125 126 return (offset); 127 } 128 129 #define SGL_FLAGS "\10LastElement\7EndOfBuffer\4Local\3Host2IOC\2Addr64\1EndOfList" 130 131 static void 132 print_sgl(char *buf, int offset, int numframes) 133 { 134 MPI2_SGE_SIMPLE64 *sge; 135 MPI2_SGE_CHAIN_UNION *sgc; 136 u_int i = 0, flags; 137 char *frame, tmpbuf[128]; 138 139 frame = (char *)buf; 140 sge = (MPI2_SGE_SIMPLE64 *)&frame[offset * 4]; 141 printf("SGL for command\n"); 142 143 hexdump(frame, MPS_FRAME_LEN, NULL, 0); 144 while (frame != NULL) { 145 flags = sge->FlagsLength >> MPI2_SGE_FLAGS_SHIFT; 146 bzero(tmpbuf, sizeof(tmpbuf)); 147 mps_parse_flags(flags, SGL_FLAGS, tmpbuf, sizeof(tmpbuf)); 148 printf("seg%d flags=%x %s len= 0x%06x addr=0x%016jx\n", i, 149 flags, tmpbuf, sge->FlagsLength & 0xffffff, 150 mps_to_u64(&sge->Address)); 151 if (flags & (MPI2_SGE_FLAGS_END_OF_LIST | 152 MPI2_SGE_FLAGS_END_OF_BUFFER)) 153 break; 154 sge++; 155 i++; 156 if (flags & MPI2_SGE_FLAGS_LAST_ELEMENT) { 157 sgc = (MPI2_SGE_CHAIN_UNION *)sge; 158 if ((sgc->Flags & MPI2_SGE_FLAGS_CHAIN_ELEMENT) == 0) { 159 printf("Invalid chain element\n"); 160 break; 161 } 162 bzero(tmpbuf, sizeof(tmpbuf)); 163 mps_parse_flags(sgc->Flags, SGL_FLAGS, tmpbuf, 164 sizeof(tmpbuf)); 165 if (sgc->Flags & MPI2_SGE_FLAGS_64_BIT_ADDRESSING) 166 printf("chain64 flags=0x%x %s len=0x%x " 167 "Offset=0x%x addr=0x%016jx\n", sgc->Flags, 168 tmpbuf, sgc->Length, sgc->NextChainOffset, 169 mps_to_u64(&sgc->u.Address64)); 170 else 171 printf("chain32 flags=0x%x %s len=0x%x " 172 "Offset=0x%x addr=0x%08x\n", sgc->Flags, 173 tmpbuf, sgc->Length, sgc->NextChainOffset, 174 sgc->u.Address32); 175 if (--numframes <= 0) 176 break; 177 frame += MPS_FRAME_LEN; 178 sge = (MPI2_SGE_SIMPLE64 *)frame; 179 hexdump(frame, MPS_FRAME_LEN, NULL, 0); 180 } 181 } 182 } 183 184 MPS_COMMAND(debug, dumpreqs, debug_dumpreqs, "", "Dump the active request queue") 185