source: trunk/src/os2ahci/trace.c@ 154

Last change on this file since 154 was 154, checked in by David Azarewicz, 12 years ago

Begin adding user info output.
Added LVM support.

File size: 8.1 KB
Line 
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 *
7 * Authors: Christian Mueller, Markus Thielen
8 *
9 * Parts copied from/inspired by the Linux AHCI driver;
10 * those parts are (c) Linux AHCI/ATA maintainers
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27#include "os2ahci.h"
28
29/* -------------------------- macros and constants ------------------------- */
30
31/* ------------------------ typedefs and structures ------------------------ */
32
33/* -------------------------- function prototypes -------------------------- */
34
35/* ------------------------ global/static variables ------------------------ */
36
37struct {
38 u32 phys_addr; /* physical address of allocated buffer */
39 u8 _far *tbuf; /* mapped address of trace buffer */
40 u16 writep; /* current write offset in buffer */
41 u16 readp; /* current read offset in buffer */
42 u16 mask; /* The mask for wrapping the buffer pointers */
43} ahci_trace_buf;
44
45/* ----------------------------- start of code ----------------------------- */
46
47/******************************************************************************
48 * initialize AHCI circular trace buffer
49 *
50 * NOTE: this func must be called during INIT time since it allocates
51 * a GDT selector for the trace ring buffer
52 */
53void trace_init(u16 usBufSize)
54{
55 SEL sel = 0;
56
57 if (ahci_trace_buf.phys_addr) return;
58
59 /* initialize ring buffer logic */
60 ahci_trace_buf.writep = 0;
61 ahci_trace_buf.readp = 0;
62 ahci_trace_buf.mask = usBufSize - 1;
63
64 if (ahci_trace_buf.phys_addr == 0) {
65 /* allocate buffer */
66 if (DevHelp_AllocPhys((ULONG) usBufSize, MEMTYPE_ABOVE_1M,
67 &(ahci_trace_buf.phys_addr))) {
68 /* failed above 1MB, try below */
69 if (DevHelp_AllocPhys((ULONG) usBufSize, MEMTYPE_BELOW_1M,
70 &(ahci_trace_buf.phys_addr))) {
71 /* failed, too. Give up */
72 ahci_trace_buf.phys_addr = 0;
73 cprintf("%s warning: failed to allocate %dk trace buffer\n",
74 drv_name, usBufSize / 1024);
75 return;
76 }
77 }
78
79 /* allocate GDT selector and map our physical trace buffer to it */
80 if (DevHelp_AllocGDTSelector(&sel, 1) ||
81 DevHelp_PhysToGDTSelector(ahci_trace_buf.phys_addr,
82 usBufSize, sel)) {
83 /* failed; free GDT selector and physical memory we allocated before */
84 if (sel) {
85 DevHelp_FreeGDTSelector(sel);
86 sel = 0;
87 }
88 DevHelp_FreePhys(ahci_trace_buf.phys_addr);
89 ahci_trace_buf.phys_addr = 0;
90 return;
91 }
92
93 /* create ring buffer address */
94 ahci_trace_buf.tbuf = (u8 _far *) ((u32) sel << 16);
95
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 */
105void 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 */
128void 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 /* The following line causes filling the trace buffer to stop when full.
139 * Comment the following line to keep filling and see the last buffer of data.
140 * Uncomment the following line to stop filling / see the first buffer of data.
141 */
142 if ( ((ahci_trace_buf.writep+1) & ahci_trace_buf.mask) == ahci_trace_buf.readp ) break; /* buffer is full */
143
144 ahci_trace_buf.tbuf[ahci_trace_buf.writep] = *s++;
145 ahci_trace_buf.writep++;
146 ahci_trace_buf.writep &= ahci_trace_buf.mask;
147
148 /* keep the latest full buffer of information */
149 if (ahci_trace_buf.writep == ahci_trace_buf.readp)
150 ahci_trace_buf.readp = (ahci_trace_buf.readp+1) & ahci_trace_buf.mask;
151
152 len--;
153 }
154
155 /* wake up processes waiting for data from trace buffer */
156 //NOT_USED DevHelp_ProcRun(ahci_trace_buf.phys_addr, &awake_cnt);
157
158}
159
160/******************************************************************************
161 * read data from circular trace buffer
162 * returns the number of bytes written to the caller's buffer
163 *
164 * NOTE: the caller is expected to call this func repeatedly
165 * (up to two times) until it returns 0
166 */
167u16 trace_read(u8 _far *buf, u16 cb_buf)
168{
169 u16 cb_read;
170
171 if (ahci_trace_buf.phys_addr == NULL) return 0;
172
173 for (cb_read = 0; cb_read < cb_buf && ( ahci_trace_buf.readp != ahci_trace_buf.writep ); cb_read++)
174 {
175 *buf++ = ahci_trace_buf.tbuf[ahci_trace_buf.readp];
176 ahci_trace_buf.readp++;
177 ahci_trace_buf.readp &= ahci_trace_buf.mask;
178 }
179
180 return cb_read;
181}
182
183/******************************************************************************
184 * copy trace buffer content to character device reader (request block buffer)
185 */
186u16 trace_char_dev(RP_RWV _far *rwrb)
187{
188 u8 _far *to_buf;
189 u16 cb_read = 0;
190 u16 cb;
191 USHORT mode = 0;
192
193 spin_lock(com_lock);
194 #ifdef NOT_USED
195 /* block process until data is available in the trace buffer. */
196 while (ahci_trace_buf.writep == ahci_trace_buf.readp ) { /* buffer is empty */
197 spin_lock(com_lock);
198 #ifndef OS2AHCI_SMP
199 com_lock = 0;
200 #endif
201 if (DevHelp_ProcBlock((ULONG) ahci_trace_buf.phys_addr,
202 (ULONG) -1, 0) == WAIT_INTERRUPTED) {
203 /* user pressed Ctrl+C or whatever */
204 rwrb->NumSectors = 0;
205 return STDON;
206 }
207 spin_lock(com_lock);
208 }
209 #endif
210
211 /* get pointer to caller's buffer */
212 if (DevHelp_PhysToVirt(rwrb->XferAddr, rwrb->NumSectors, &to_buf, &mode)) {
213 spin_unlock(com_lock);
214 return (STATUS_DONE | STERR);
215 }
216
217 /* loop until caller's buffer is full or no more data in trace buffer */
218 do {
219 cb = trace_read(to_buf + cb_read, rwrb->NumSectors - cb_read);
220 cb_read += cb;
221 } while (cb > 0 && cb_read < rwrb->NumSectors);
222
223 spin_unlock(com_lock);
224 rwrb->NumSectors = cb_read;
225
226 return(STDON);
227}
228
229/******************************************************************************
230 * Create adapter/port/device list for user output.
231 */
232void build_user_info(void)
233{
234 int a;
235 int p;
236 int d;
237
238 for (a = 0; a < ad_info_cnt; a++) {
239 AD_INFO *ai = ad_infos + a;
240
241 printf_nts("Adapter %d: %d:%d:%d irq=%d addr=0x%lx\n", a,
242 ai->bus, ai->dev_func>>3, ai->dev_func&7,
243 ai->irq, ai->mmio_phys);
244
245 for (p = 0; p <= ai->port_max; p++) {
246 P_INFO *pi = &ai->ports[p];
247
248 printf_nts(" Port %d:\n", p);
249
250 for (d = 0; d <= pi->dev_max; d++) {
251 if (!pi->devs[d].present) continue;
252 printf_nts(" Drive %d: atapi=%d, removable=%d\n", d, pi->devs[d].atapi, pi->devs[d].removable);
253 }
254 }
255 }
256}
257
Note: See TracBrowser for help on using the repository browser.