| 1 | /******************************************************************************
|
|---|
| 2 | * trace.c - code for our internal trace ring buffer
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright (c) 2011 thi.guten Software Development
|
|---|
| 5 | * Copyright (c) 2011 Mensys B.V.
|
|---|
| 6 | * Copyright (c) 2013-2015 David Azarewicz
|
|---|
| 7 | *
|
|---|
| 8 | * Authors: Christian Mueller, Markus Thielen
|
|---|
| 9 | *
|
|---|
| 10 | * Parts copied from/inspired by the Linux AHCI driver;
|
|---|
| 11 | * those parts are (c) Linux AHCI/ATA maintainers
|
|---|
| 12 | *
|
|---|
| 13 | * This program is free software; you can redistribute it and/or modify
|
|---|
| 14 | * it under the terms of the GNU General Public License as published by
|
|---|
| 15 | * the Free Software Foundation; either version 2 of the License, or
|
|---|
| 16 | * (at your option) any later version.
|
|---|
| 17 | *
|
|---|
| 18 | * This program is distributed in the hope that it will be useful,
|
|---|
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 21 | * GNU General Public License for more details.
|
|---|
| 22 | *
|
|---|
| 23 | * You should have received a copy of the GNU General Public License
|
|---|
| 24 | * along with this program; if not, write to the Free Software
|
|---|
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | #include "os2ahci.h"
|
|---|
| 29 |
|
|---|
| 30 | /* -------------------------- macros and constants ------------------------- */
|
|---|
| 31 |
|
|---|
| 32 | /* ------------------------ typedefs and structures ------------------------ */
|
|---|
| 33 |
|
|---|
| 34 | /* -------------------------- function prototypes -------------------------- */
|
|---|
| 35 |
|
|---|
| 36 | /* ------------------------ global/static variables ------------------------ */
|
|---|
| 37 |
|
|---|
| 38 | struct {
|
|---|
| 39 | u32 phys_addr; /* physical address of allocated buffer */
|
|---|
| 40 | u8 _far *tbuf; /* mapped address of trace buffer */
|
|---|
| 41 | u16 writep; /* current write offset in buffer */
|
|---|
| 42 | u16 readp; /* current read offset in buffer */
|
|---|
| 43 | u16 mask; /* The mask for wrapping the buffer pointers */
|
|---|
| 44 | } ahci_trace_buf;
|
|---|
| 45 |
|
|---|
| 46 | /* ----------------------------- start of code ----------------------------- */
|
|---|
| 47 |
|
|---|
| 48 | /******************************************************************************
|
|---|
| 49 | * initialize AHCI circular trace buffer
|
|---|
| 50 | *
|
|---|
| 51 | * NOTE: this func must be called during INIT time since it allocates
|
|---|
| 52 | * a GDT selector for the trace ring buffer
|
|---|
| 53 | */
|
|---|
| 54 | void trace_init(u32 ulBufSize)
|
|---|
| 55 | {
|
|---|
| 56 | SEL sel = 0;
|
|---|
| 57 |
|
|---|
| 58 | if (ahci_trace_buf.phys_addr) return;
|
|---|
| 59 |
|
|---|
| 60 | /* initialize ring buffer logic */
|
|---|
| 61 | ahci_trace_buf.writep = 0;
|
|---|
| 62 | ahci_trace_buf.readp = 0;
|
|---|
| 63 | ahci_trace_buf.mask = ulBufSize - 1;
|
|---|
| 64 |
|
|---|
| 65 | if (ahci_trace_buf.phys_addr == 0) {
|
|---|
| 66 | /* allocate buffer */
|
|---|
| 67 | if (DevHelp_AllocPhys(ulBufSize, MEMTYPE_ABOVE_1M,
|
|---|
| 68 | &(ahci_trace_buf.phys_addr))) {
|
|---|
| 69 | /* failed above 1MB, try below */
|
|---|
| 70 | if (DevHelp_AllocPhys(ulBufSize, MEMTYPE_BELOW_1M,
|
|---|
| 71 | &(ahci_trace_buf.phys_addr))) {
|
|---|
| 72 | /* failed, too. Give up */
|
|---|
| 73 | ahci_trace_buf.phys_addr = 0;
|
|---|
| 74 | cprintf("%s warning: failed to allocate %dk trace buffer\n",
|
|---|
| 75 | drv_name, ulBufSize / 1024);
|
|---|
| 76 | return;
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /* allocate GDT selector and map our physical trace buffer to it */
|
|---|
| 81 | if (DevHelp_AllocGDTSelector(&sel, 1) ||
|
|---|
| 82 | DevHelp_PhysToGDTSelector(ahci_trace_buf.phys_addr,
|
|---|
| 83 | ulBufSize, sel)) {
|
|---|
| 84 | /* failed; free GDT selector and physical memory we allocated before */
|
|---|
| 85 | if (sel) {
|
|---|
| 86 | DevHelp_FreeGDTSelector(sel);
|
|---|
| 87 | sel = 0;
|
|---|
| 88 | }
|
|---|
| 89 | DevHelp_FreePhys(ahci_trace_buf.phys_addr);
|
|---|
| 90 | ahci_trace_buf.phys_addr = 0;
|
|---|
| 91 | return;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /* create ring buffer address */
|
|---|
| 95 | ahci_trace_buf.tbuf = (u8 _far *) ((u32) sel << 16);
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /******************************************************************************
|
|---|
| 100 | * cleanup trace buffer
|
|---|
| 101 | *
|
|---|
| 102 | * NOTE: this function is here for completeness; the trace buffer should not
|
|---|
| 103 | * be deallocated and then reallocated.
|
|---|
| 104 | */
|
|---|
| 105 | void trace_exit(void)
|
|---|
| 106 | {
|
|---|
| 107 | /* free physical address */
|
|---|
| 108 | if (ahci_trace_buf.phys_addr) {
|
|---|
| 109 | DevHelp_FreePhys(ahci_trace_buf.phys_addr);
|
|---|
| 110 | ahci_trace_buf.phys_addr = 0;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /* free GDT selector */
|
|---|
| 114 | if (ahci_trace_buf.tbuf) {
|
|---|
| 115 | DevHelp_FreeGDTSelector((SEL) ((u32) (ahci_trace_buf.tbuf) >> 16));
|
|---|
| 116 | ahci_trace_buf.tbuf = NULL;
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 | /******************************************************************************
|
|---|
| 122 | * write a string to the circular trace buffer
|
|---|
| 123 | *
|
|---|
| 124 | * Note: This func wraps the buffer if necessary, so the caller does not
|
|---|
| 125 | * need to call repeatedly until everything is written.
|
|---|
| 126 | *
|
|---|
| 127 | */
|
|---|
| 128 | void trace_write(u8 _far *s, int len)
|
|---|
| 129 | {
|
|---|
| 130 | //NOT USED USHORT awake_cnt;
|
|---|
| 131 |
|
|---|
| 132 | if (ahci_trace_buf.phys_addr == 0) {
|
|---|
| 133 | /* tracing not active */
|
|---|
| 134 | return;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | while (len) {
|
|---|
| 138 | if ( !wrap_trace_buffer && (((ahci_trace_buf.writep+1) & ahci_trace_buf.mask) == ahci_trace_buf.readp) ) break; /* buffer is full */
|
|---|
| 139 |
|
|---|
| 140 | ahci_trace_buf.tbuf[ahci_trace_buf.writep] = *s++;
|
|---|
| 141 | ahci_trace_buf.writep++;
|
|---|
| 142 | ahci_trace_buf.writep &= ahci_trace_buf.mask;
|
|---|
| 143 |
|
|---|
| 144 | /* keep the latest full buffer of information */
|
|---|
| 145 | if (ahci_trace_buf.writep == ahci_trace_buf.readp)
|
|---|
| 146 | ahci_trace_buf.readp = (ahci_trace_buf.readp+1) & ahci_trace_buf.mask;
|
|---|
| 147 |
|
|---|
| 148 | len--;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /* wake up processes waiting for data from trace buffer */
|
|---|
| 152 | //NOT_USED DevHelp_ProcRun(ahci_trace_buf.phys_addr, &awake_cnt);
|
|---|
| 153 |
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | /******************************************************************************
|
|---|
| 157 | * read data from circular trace buffer
|
|---|
| 158 | * returns the number of bytes written to the caller's buffer
|
|---|
| 159 | *
|
|---|
| 160 | * NOTE: the caller is expected to call this func repeatedly
|
|---|
| 161 | * (up to two times) until it returns 0
|
|---|
| 162 | */
|
|---|
| 163 | u16 trace_read(u8 _far *buf, u16 cb_buf)
|
|---|
| 164 | {
|
|---|
| 165 | u16 cb_read;
|
|---|
| 166 |
|
|---|
| 167 | if (ahci_trace_buf.phys_addr == NULL) return 0;
|
|---|
| 168 |
|
|---|
| 169 | for (cb_read = 0; cb_read < cb_buf && ( ahci_trace_buf.readp != ahci_trace_buf.writep ); cb_read++)
|
|---|
| 170 | {
|
|---|
| 171 | *buf++ = ahci_trace_buf.tbuf[ahci_trace_buf.readp];
|
|---|
| 172 | ahci_trace_buf.readp++;
|
|---|
| 173 | ahci_trace_buf.readp &= ahci_trace_buf.mask;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | return cb_read;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | /******************************************************************************
|
|---|
| 180 | * copy trace buffer content to character device reader (request block buffer)
|
|---|
| 181 | */
|
|---|
| 182 | u16 trace_char_dev(RP_RWV _far *rwrb)
|
|---|
| 183 | {
|
|---|
| 184 | u8 _far *to_buf;
|
|---|
| 185 | u16 cb_read = 0;
|
|---|
| 186 | u16 cb;
|
|---|
| 187 | USHORT mode = 0;
|
|---|
| 188 |
|
|---|
| 189 | spin_lock(com_lock);
|
|---|
| 190 |
|
|---|
| 191 | /* get pointer to caller's buffer */
|
|---|
| 192 | if (DevHelp_PhysToVirt(rwrb->XferAddr, rwrb->NumSectors, &to_buf, &mode)) {
|
|---|
| 193 | spin_unlock(com_lock);
|
|---|
| 194 | return (STATUS_DONE | STERR);
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | /* loop until caller's buffer is full or no more data in trace buffer */
|
|---|
| 198 | do {
|
|---|
| 199 | cb = trace_read(to_buf + cb_read, rwrb->NumSectors - cb_read);
|
|---|
| 200 | cb_read += cb;
|
|---|
| 201 | } while (cb > 0 && cb_read < rwrb->NumSectors);
|
|---|
| 202 |
|
|---|
| 203 | spin_unlock(com_lock);
|
|---|
| 204 | rwrb->NumSectors = cb_read;
|
|---|
| 205 |
|
|---|
| 206 | return(STDON);
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | /******************************************************************************
|
|---|
| 210 | * Create adapter/port/device list for user output.
|
|---|
| 211 | */
|
|---|
| 212 | void build_user_info(int check)
|
|---|
| 213 | {
|
|---|
| 214 | int a;
|
|---|
| 215 | int p;
|
|---|
| 216 | int d;
|
|---|
| 217 |
|
|---|
| 218 | if ( check && (ahci_trace_buf.readp != ahci_trace_buf.writep)) return;
|
|---|
| 219 |
|
|---|
| 220 | for (a = 0; a < ad_info_cnt; a++) {
|
|---|
| 221 | AD_INFO *ai = ad_infos + a;
|
|---|
| 222 |
|
|---|
| 223 | ntprintf("Adapter %d: PCI=%d:%d:%d ID=%04x:%04x %s %s irq=%d addr=0x%lx version=%lx\n", a,
|
|---|
| 224 | ai->bus, ai->dev_func>>3, ai->dev_func&7,
|
|---|
| 225 | ai->pci_vendor, ai->pci_device, vendor_from_id(ai->pci_vendor), ai->pci->chipname,
|
|---|
| 226 | ai->irq, ai->mmio_phys,
|
|---|
| 227 | ai->bios_config[HOST_VERSION / sizeof(u32)]);
|
|---|
| 228 |
|
|---|
| 229 | for (p = 0; p < ai->hw_ports; p++) {
|
|---|
| 230 | P_INFO *pi = &ai->ports[p];
|
|---|
| 231 |
|
|---|
| 232 | ntprintf(" Port %d:\n", p);
|
|---|
| 233 |
|
|---|
| 234 | for (d = 0; d <= pi->dev_max; d++) {
|
|---|
| 235 | if (!pi->devs[d].present) {
|
|---|
| 236 | ntprintf(" No drive present\n");
|
|---|
| 237 | } else {
|
|---|
| 238 | ntprintf(" Drive %d:", d);
|
|---|
| 239 | if (pi->devs[d].atapi) ntprintf(" atapi");
|
|---|
| 240 | if (pi->devs[d].removable) ntprintf(" removable");
|
|---|
| 241 | if (pi->devs[d].dev_info.Method != NULL) {
|
|---|
| 242 | ntprintf(" %ld cylinders, %d heads, %d sectors per track (%ldMB) (%s)",
|
|---|
| 243 | (u32)pi->devs[d].dev_info.Cylinders, pi->devs[d].dev_info.HeadsPerCylinder, pi->devs[d].dev_info.SectorsPerTrack,
|
|---|
| 244 | pi->devs[d].dev_info.TotalSectors/2048, pi->devs[d].dev_info.Method);
|
|---|
| 245 | } else ntprintf(" Drive present but no information available. Not queried by OS.");
|
|---|
| 246 | ntprintf("\n");
|
|---|
| 247 | } /* if */
|
|---|
| 248 | } /* for d */
|
|---|
| 249 | } /* for p */
|
|---|
| 250 | } /* for a */
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|