source: trunk/src/os2ahci/os2ahci.c@ 160

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

fixed trap dump kernel exit, some work on suspend/resume routines

File size: 51.8 KB
Line 
1/******************************************************************************
2 * os2ahci.c - main file for os2ahci driver
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#include "ioctl.h"
29#include "version.h"
30
31/* -------------------------- macros and constants ------------------------- */
32
33/* parse integer command line parameter */
34#define drv_parm_int(s, value, type, radix) \
35 { \
36 char _far *_ep; \
37 if ((s)[1] != ':') { \
38 cprintf("%s: missing colon (:) after /%c\n", drv_name, *(s)); \
39 goto init_fail; \
40 } \
41 value = (type) strtol((s) + 2, \
42 (const char _far* _far*) &_ep, \
43 radix); \
44 s = _ep; \
45 }
46
47#define drv_parm_int_optional(s, value, type, radix) \
48 { \
49 char _far *_ep; \
50 if ((s)[1] == ':') { \
51 value = (type) strtol((s) + 2, (const char _far* _far*) &_ep, radix); \
52 s = _ep; \
53 } else { \
54 value++; \
55 } \
56 }
57
58/* set two-dimensional array of port options */
59#define set_port_option(opt, val) \
60 if (adapter_index == -1) { \
61 /* set option for all adapters and ports */ \
62 memset(opt, val, sizeof(opt)); \
63 } else if (port_index == -1) { \
64 /* set option for all ports on current adapter */ \
65 memset(opt[adapter_index], val, sizeof(*opt)); \
66 } else { \
67 /* set option for specific port */ \
68 opt[adapter_index][port_index] = val; \
69 }
70
71/* constants for undefined kernel exit routine;
72 * see register_krnl_exit() func */
73#define DevHlp_RegisterKrnlExit 0x006f
74
75#define FLAG_KRNL_EXIT_ADD 0x1000
76#define FLAG_KRNL_EXIT_REMOVE 0x2000
77
78#define TYPE_KRNL_EXIT_NMI 0x0000 /* non masked interrupts */
79#define TYPE_KRNL_EXIT_SFF 0x0001 /* system fatal faults */
80#define TYPE_KRNL_EXIT_PROCDUMP 0x0002
81#define TYPE_KRNL_EXIT_DYN 0x0003
82#define TYPE_KRNL_EXIT_INT13 0x0004 /* enable int13 IO */
83
84/* ------------------------ typedefs and structures ------------------------ */
85
86/* -------------------------- function prototypes -------------------------- */
87
88void _cdecl small_code_ (void);
89
90static int add_unit_info (IORB_CONFIGURATION _far *iorb_conf, int dt_ai,
91 int a, int p, int d, int scsi_id);
92
93static void register_krnl_exit (void);
94
95/* ------------------------ global/static variables ------------------------ */
96
97int debug = 0; /* if > 0, print debug messages to COM1 */
98int thorough_scan = 1; /* if != 0, perform thorough PCI scan */
99int init_reset = 1; /* if != 0, reset ports during init */
100int force_write_cache; /* if != 0, force write cache */
101int verbosity = 0; /* default is quiet. 1=show sign on banner, >1=show adapter info during boot */
102int use_lvm_info = 1;
103int wrap_trace_buffer = 0;
104long com_baud = 0;
105
106PFN Device_Help = 0; /* pointer to device helper entry point */
107ULONG RMFlags = 0; /* required by resource manager library */
108PFN RM_Help0 = NULL; /* required by resource manager library */
109PFN RM_Help3 = NULL; /* required by resource manager library */
110HDRIVER rm_drvh; /* resource manager driver handle */
111char rm_drvname[80]; /* driver name as returned by RM */
112USHORT add_handle; /* driver handle (RegisterDeviceClass) */
113UCHAR timer_pool[TIMER_POOL_SIZE]; /* timer pool */
114char drv_name[] = "OS2AHCI"; /* driver name as string */
115
116/* resource manager driver information structure */
117DRIVERSTRUCT rm_drvinfo = {
118 drv_name, /* driver name */
119 "AHCI SATA Driver", /* driver description */
120 DVENDOR, /* vendor name */
121 CMVERSION_MAJOR, /* RM interface version major */
122 CMVERSION_MINOR, /* RM interface version minor */
123 BLD_YEAR, BLD_MONTH, BLD_DAY, /* date */
124 0, /* driver flags */
125 DRT_ADDDM, /* driver type */
126 DRS_ADD, /* driver sub type */
127 NULL /* driver callback */
128};
129
130ULONG drv_lock; /* driver-level spinlock */
131IORB_QUEUE driver_queue; /* driver-level IORB queue */
132AD_INFO ad_infos[MAX_AD]; /* adapter information list */
133int ad_info_cnt; /* number of entries in ad_infos[] */
134u16 ad_ignore; /* bitmap with adapter indexes to ignore */
135int init_complete; /* if != 0, initialization has completed */
136int suspended;
137
138/* apapter/port-specific options saved when parsing the command line */
139u8 emulate_scsi[MAX_AD][AHCI_MAX_PORTS];
140u8 enable_ncq[MAX_AD][AHCI_MAX_PORTS];
141u8 link_speed[MAX_AD][AHCI_MAX_PORTS];
142u8 link_power[MAX_AD][AHCI_MAX_PORTS];
143u8 track_size[MAX_AD][AHCI_MAX_PORTS];
144
145static char init_msg[] = "%s driver version %d.%02d\n";
146static char exit_msg[] = "%s driver *not* installed\n";
147char BldLevel[] = BLDLEVEL;
148
149/* ----------------------------- start of code ----------------------------- */
150
151/******************************************************************************
152 * OS/2 device driver main strategy function. This function is only used
153 * for initialization purposes; all other calls go directly to the adapter
154 * device driver's strategy function.
155 *
156 * NOTE: this is also used as the IDC entry point. We expect an IOCTL request
157 * packet for IDC calls, so they can be handled by gen_ioctl.
158 */
159USHORT _cdecl c_strat(RPH _far *req)
160{
161 u16 rc;
162
163 switch (req->Cmd) {
164
165 case CMDInitBase:
166 rc = init_drv((RPINITIN _far *) req);
167 break;
168
169 case CMDShutdown:
170 rc = exit_drv(((RPSAVERESTORE _far *) req)->FuncCode);
171 break;
172
173 case CMDGenIOCTL:
174 rc = gen_ioctl((RP_GENIOCTL _far *) req);
175 break;
176
177 case CMDINPUT:
178 rc = char_dev_input((RP_RWV _far *) req);
179 break;
180
181 case CMDSaveRestore:
182 rc = sr_drv(((RPSAVERESTORE _far *) req)->FuncCode);
183 break;
184
185 default:
186 rc = STDON | STATUS_ERR_UNKCMD;
187 break;
188 }
189
190 return(rc);
191}
192
193/******************************************************************************
194 * Intialize the os2ahci driver. This includes command line parsing, scanning
195 * the PCI bus for supported AHCI adapters, etc.
196 */
197USHORT init_drv(RPINITIN _far *req)
198{
199 static int init_drv_called;
200 static int init_drv_failed;
201 RPINITOUT _far *rsp = (RPINITOUT _far *) req;
202 DDD_PARM_LIST _far *ddd_pl = (DDD_PARM_LIST _far *) req->InitArgs;
203 APIRET rmrc;
204 char _far *cmd_line;
205 char _far *s;
206 int adapter_index = -1;
207 int port_index = -1;
208 int invert_option;
209 int optval;
210 u16 vendor;
211 u16 device;
212
213 if (init_drv_called) {
214 /* This is the init call for the second (legacy IBMS506$) character
215 * device driver. If the main driver failed initialization, fail this
216 * one as well.
217 */
218 rsp->CodeEnd = (u16) end_of_code;
219 rsp->DataEnd = (u16) &end_of_data;
220 return(STDON | ((init_drv_failed) ? ERROR_I24_QUIET_INIT_FAIL : 0));
221 }
222 init_drv_called = 1;
223 suspended = 0;
224
225 /* set device helper entry point */
226 Device_Help = req->DevHlpEP;
227
228 /* create driver-level spinlock */
229 DevHelp_CreateSpinLock(&drv_lock);
230
231 /* initialize libc code */
232 init_libc();
233
234 /* register driver with resource manager */
235 if ((rmrc = RMCreateDriver(&rm_drvinfo, &rm_drvh)) != RMRC_SUCCESS) {
236 cprintf("%s: failed to register driver with resource manager (rc = %d)\n",
237 drv_name, rmrc);
238 goto init_fail;
239 }
240
241 /* parse command line parameters */
242 cmd_line = (char _far *) ((u32) ddd_pl & 0xffff0000l) + ddd_pl->cmd_line_args;
243
244 for (s = cmd_line; *s != 0; s++) {
245 if (*s == '/') {
246 if ((invert_option = (s[1] == '!')) != 0) {
247 s++;
248 }
249 s++;
250 switch (tolower(*s)) {
251
252 case '\0':
253 /* end of command line; can only happen if command line is incorrect */
254 cprintf("%s: incomplete command line option\n", drv_name);
255 goto init_fail;
256
257 case 'b':
258 drv_parm_int(s, com_baud, u32, 10);
259 break;
260
261 case 'c':
262 /* set COM port base address for debug messages */
263 drv_parm_int(s, com_base, u16, 16);
264 if (com_base == 1) com_base = 0x3f8;
265 if (com_base == 2) com_base = 0x2f8;
266 break;
267
268 case 'd':
269 /* increase debug level */
270 drv_parm_int_optional(s, debug, int, 10);
271 break;
272
273 case 'g':
274 /* add specfied PCI ID as a supported generic AHCI adapter */
275 drv_parm_int(s, vendor, u16, 16);
276 s--;
277 drv_parm_int(s, device, u16, 16);
278 if (add_pci_id(vendor, device)) {
279 cprintf("%s: failed to add PCI ID %04x:%04x\n", drv_name, vendor, device);
280 goto init_fail;
281 }
282 thorough_scan = 1;
283 break;
284
285 case 't':
286 /* perform thorough PCI scan (i.e. look for individual supported PCI IDs) */
287 thorough_scan = !invert_option;
288 break;
289
290 case 'r':
291 /* reset ports during initialization */
292 init_reset = !invert_option;
293 break;
294
295 case 'f':
296 /* force write cache regardless of IORB flags */
297 force_write_cache = 1;
298 break;
299
300 case 'a':
301 /* set adapter index for adapter and port-related options */
302 drv_parm_int(s, adapter_index, int, 10);
303 if (adapter_index < 0 || adapter_index >= MAX_AD) {
304 cprintf("%s: invalid adapter index (%d)\n", drv_name, adapter_index);
305 goto init_fail;
306 }
307 break;
308
309 case 'p':
310 /* set port index for port-related options */
311 drv_parm_int(s, port_index, int, 10);
312 if (port_index < 0 || port_index >= AHCI_MAX_PORTS) {
313 cprintf("%s: invalid port index (%d)\n", drv_name, port_index);
314 goto init_fail;
315 }
316 break;
317
318 case 'i':
319 /* ignore current adapter index */
320 if (adapter_index >= 0) {
321 ad_ignore |= 1U << adapter_index;
322 }
323 break;
324
325 case 's':
326 /* enable SCSI emulation for ATAPI devices */
327 set_port_option(emulate_scsi, !invert_option);
328 break;
329
330 case 'n':
331 /* enable NCQ */
332 set_port_option(enable_ncq, !invert_option);
333 break;
334
335 case 'l':
336 /* set link speed or power savings */
337 s++;
338 switch (tolower(*s)) {
339 case 's':
340 /* set link speed */
341 drv_parm_int(s, optval, int, 10);
342 set_port_option(link_speed, optval);
343 break;
344 case 'p':
345 /* set power management */
346 drv_parm_int(s, optval, int, 10);
347 set_port_option(link_power, optval);
348 break;
349 default:
350 cprintf("%s: invalid link parameter (%c)\n", drv_name, *s);
351 goto init_fail;
352 }
353 /* need to reset the port in order to establish link settings */
354 init_reset = 1;
355 break;
356
357 case '4':
358 /* enable 4K sector geometry enhancement (track size = 56) */
359 if (!invert_option) {
360 set_port_option(track_size, 56);
361 }
362 break;
363
364 case 'z':
365 /* Specify to not use the LVM information. There is no reason why anyone would
366 * want to do this, but previous versions of this driver did not have LVM capability,
367 * so this switch is here temporarily just in case.
368 */
369 use_lvm_info = !invert_option;
370 break;
371
372 case 'v':
373 /* be verbose during boot */
374 drv_parm_int_optional(s, verbosity, int, 10);
375 break;
376
377 case 'w':
378 /* Specify to allow the trace buffer to wrap when full. */
379 wrap_trace_buffer = !invert_option;
380 break;
381
382 case 'q':
383 /* Temporarily output a non-fatal message to get anyone using this
384 * undocumented switch to stop using it. This will be removed soon
385 * and the error will become fatal.
386 */
387 cprintf("%s: unknown option: /%c\n", drv_name, *s);
388 break;
389
390 default:
391 cprintf("%s: unknown option: /%c\n", drv_name, *s);
392 goto init_fail;
393 }
394 }
395 }
396
397 /* print initialization message */
398 ciprintf(init_msg, drv_name, VERSION / 100, VERSION % 100);
399
400 if (com_baud) init_com(com_baud); /* initialize com port for debug output */
401
402 /* initialize trace buffer if applicable */
403 if (TRACE_ACTIVE) {
404 /* debug is on, but COM port is off -> use our trace buffer */
405 trace_init(AHCI_TRACE_BUF_SIZE);
406 } else {
407 trace_init(AHCI_INFO_BUF_SIZE);
408 }
409
410 ntprintf("BldLevel: %s\n", BldLevel);
411 ntprintf("CmdLine: %Fs\n", cmd_line);
412
413 /* scan PCI bus for supported devices */
414 scan_pci_bus();
415
416 if (ad_info_cnt > 0) {
417 /* initialization succeeded and we found at least one AHCI adapter */
418 ADD_InitTimer(timer_pool, sizeof(timer_pool));
419 //NOT_USED mdelay_cal();
420
421 if (DevHelp_RegisterDeviceClass(drv_name, (PFN) add_entry, 0, 1, &add_handle)) {
422 cprintf("%s: couldn't register device class\n", drv_name);
423 goto init_fail;
424 }
425
426 /* allocate context hooks */
427 if (DevHelp_AllocateCtxHook(mk_NPFN(restart_hook), &restart_ctxhook_h) != 0 ||
428 DevHelp_AllocateCtxHook(mk_NPFN(reset_hook), &reset_ctxhook_h) != 0 ||
429 DevHelp_AllocateCtxHook(mk_NPFN(engine_hook), &engine_ctxhook_h)) {
430 cprintf("%s: failed to allocate task-time context hooks\n", drv_name);
431 goto init_fail;
432 }
433
434 rsp->CodeEnd = (u16) end_of_code;
435 rsp->DataEnd = (u16) &end_of_data;
436
437 /* register kernel exit routine for trap dumps */
438 register_krnl_exit();
439
440 return(STDON);
441
442 } else {
443 /* no adapters found */
444 ciprintf(" No adapters found.\n");
445 }
446
447init_fail:
448 /* initialization failed; set segment sizes to 0 and return error */
449 rsp->CodeEnd = 0;
450 rsp->DataEnd = 0;
451 init_drv_failed = 1;
452
453 /* free context hooks */
454 if (engine_ctxhook_h != 0) DevHelp_FreeCtxHook(engine_ctxhook_h);
455 if (reset_ctxhook_h != 0) DevHelp_FreeCtxHook(reset_ctxhook_h);
456 if (restart_ctxhook_h != 0) DevHelp_FreeCtxHook(restart_ctxhook_h);
457
458 if (rm_drvh != 0) {
459 /* remove driver from resource manager */
460 RMDestroyDriver(rm_drvh);
461 }
462
463 ciprintf(exit_msg, drv_name);
464 return(STDON | ERROR_I24_QUIET_INIT_FAIL);
465}
466
467/******************************************************************************
468 * Generic IOCTL via character device driver. IOCTLs are used to control the
469 * driver operation and to execute native ATA and ATAPI (SCSI) commands from
470 * ring 3 applications. On top of that, some predefined IOCTLs (e.g. SMART
471 * commands for ATA disks) are implemented here.
472 */
473USHORT gen_ioctl(RP_GENIOCTL _far *ioctl)
474{
475 dprintf("IOCTL 0x%x/0x%x\n", (u16) ioctl->Category, (u16) ioctl->Function);
476
477 switch (ioctl->Category) {
478
479 case OS2AHCI_IOCTL_CATEGORY:
480 switch (ioctl->Function) {
481
482 case OS2AHCI_IOCTL_GET_DEVLIST:
483 return(ioctl_get_devlist(ioctl));
484
485 case OS2AHCI_IOCTL_PASSTHROUGH:
486 return(ioctl_passthrough(ioctl));
487
488 }
489 break;
490
491 case DSKSP_CAT_GENERIC:
492 return(ioctl_gen_dsk(ioctl));
493
494 case DSKSP_CAT_SMART:
495 return(ioctl_smart(ioctl));
496
497 }
498
499 return(STDON | STATUS_ERR_UNKCMD);
500}
501
502/******************************************************************************
503 * Read from character device. If tracing is on (internal ring buffer trace),
504 * we return data from the trace buffer; if not, we might return a device
505 * dump similar to IBM1S506.ADD/DANIS506.ADD (TODO).
506 */
507USHORT char_dev_input(RP_RWV _far *rwrb)
508{
509 return(trace_char_dev(rwrb));
510}
511
512/******************************************************************************
513 * Device driver exit handler. This handler is called when OS/2 shuts down and
514 * flushes the write caches of all attached devices. Since this is effectively
515 * the same we do when suspending, we'll call out to the corresponding APM
516 * function.
517 *
518 * NOTE: Errors are ignored because there's no way we could stop the shutdown
519 * or do something about the error, unless retrying endlessly is
520 * considered an option.
521 */
522USHORT exit_drv(int func)
523{
524 dprintf("exit_drv(%d) called\n", func);
525
526 if (func == 0) {
527 /* we're only interested in the second phase of the shutdown */
528 return(STDON);
529 }
530
531 suspend();
532 return(STDON);
533}
534
535/******************************************************************************
536 * Device driver suspend/resume handler. This handler is called when ACPI is
537 * executing a suspend or resume.
538 */
539USHORT sr_drv(int func)
540{
541 dprintf("sr_drv(%d) called\n", func);
542
543 if (func) resume();
544 else suspend();
545
546 return(STDON);
547}
548
549/******************************************************************************
550 * ADD entry point. This is the main entry point for all ADD requests. Due to
551 * the asynchronous nature of ADD drivers, this function primarily queues the
552 * IORB(s) to the corresponding adapter or port queues, then triggers the
553 * state machine to initiate processing queued IORBs.
554 *
555 * NOTE: In order to prevent race conditions or engine stalls, certain rules
556 * around locking, unlocking and IORB handling in general have been
557 * established. Refer to the comments in "trigger_engine()" for
558 * details.
559 */
560void _cdecl _far _loadds add_entry(IORBH _far *first_iorb)
561{
562 IORBH _far *iorb;
563 IORBH _far *next = NULL;
564
565 spin_lock(drv_lock);
566
567 for (iorb = first_iorb; iorb != NULL; iorb = next) {
568 /* Queue this IORB. Queues primarily exist on port level but there are
569 * some requests which affect the whole driver, most notably
570 * IOCC_CONFIGURATION. In either case, adding the IORB to the driver or
571 * port queue will change the links, thus we need to save the original
572 * link in 'next'.
573 */
574 next = (iorb->RequestControl | IORB_CHAIN) ? iorb->pNxtIORB : 0;
575
576 iorb->Status = 0;
577 iorb->ErrorCode = 0;
578 memset(&iorb->ADDWorkSpace, 0x00, sizeof(ADD_WORKSPACE));
579
580 if (iorb_driver_level(iorb)) {
581 /* driver-level IORB */
582 iorb->UnitHandle = 0;
583 iorb_queue_add(&driver_queue, iorb);
584
585 } else {
586 /* port-level IORB */
587 int a = iorb_unit_adapter(iorb);
588 int p = iorb_unit_port(iorb);
589 int d = iorb_unit_device(iorb);
590
591 if (a >= ad_info_cnt ||
592 p > ad_infos[a].port_max ||
593 d > ad_infos[a].ports[p].dev_max ||
594 (ad_infos[a].port_map & (1UL << p)) == 0) {
595
596 /* unit handle outside of the allowed range */
597 dprintf("warning: IORB for %d.%d.%d out of range\n", a, p, d);
598 iorb->Status = IORB_ERROR;
599 iorb->ErrorCode = IOERR_CMD_SYNTAX;
600 iorb_complete(iorb);
601 continue;
602 }
603
604 iorb_queue_add(&ad_infos[a].ports[p].iorb_queue, iorb);
605 }
606 }
607
608 /* trigger state machine */
609 trigger_engine();
610
611 spin_unlock(drv_lock);
612}
613
614/******************************************************************************
615 * Trigger IORB queue engine. This is a wrapper function for trigger_engine_1()
616 * which will try to get all IORBs sent on their way a couple of times. If
617 * there are still IORBs ready for processing after this, this function will
618 * hand off to a context hook which will continue to trigger the engine until
619 * all IORBs have been sent.
620 *
621 * NOTE: While initialization has not completed (or during APM suspend/resume
622 * operations), this function will loop indefinitely because we can't
623 * rely on interrupt handlers or context hooks and complex IORBs
624 * requiring multiple requeues would eventually hang and time out if
625 * we stopped triggering here.
626 */
627void trigger_engine(void)
628{
629 int i;
630
631 for (i = 0; i < 3 || !init_complete; i++) {
632 if (trigger_engine_1() == 0) {
633 /* done -- all IORBs have been sent on their way */
634 return;
635 }
636 }
637
638 /* Something keeps bouncing; hand off to the engine context hook which will
639 * keep trying in the background.
640 */
641 DevHelp_ArmCtxHook(0, engine_ctxhook_h);
642}
643
644/******************************************************************************
645 * Trigger IORB queue engine in order to send commands in the driver/port IORB
646 * queues to the AHCI hardware. This function will return the number of IORBs
647 * sent. Keep in mind that IORBs might "bounce" if the adapter/port is not in
648 * a state to accept the command, thus it might take quite a few calls to get
649 * all IORBs on their way. This is why there's a wrapper function which tries
650 * it a few times, then hands off to a context hook which will keep trying in
651 * the background.
652 *
653 * IORBs might complete before send_iorb() has returned, at any time during
654 * interrupt processing or on another CPU on SMP systems. IORB completion
655 * means modifications to the corresponding IORB queue (the completed IORB
656 * is removed from the queue) thus we need to protect the IORB queues from
657 * race conditions. The safest approach short of keeping the driver-level
658 * spinlock aquired permanently is to keep it throughout this function and
659 * release it temporarily in send_iorb().
660 *
661 * This implies that the handler functions are fully responsible for aquiring
662 * the driver-level spinlock when they need it, and for releasing it again.
663 *
664 * As a rule of thumb, get the driver-level spinlock whenever accessing
665 * volatile variables (IORB queues, values in ad_info[], ...).
666 *
667 * Additional Notes:
668 *
669 * - This function is expected to be called with the spinlock aquired
670 *
671 * - Adapters can be flagged as 'busy' which means no new IORBs are sent (they
672 * just remain in the queue). This can be used to release the driver-level
673 * spinlock while making sure no new IORBs are going to hit the hardware.
674 * In order to prevent engine stalls, all handlers using this functionality
675 * need to invoke trigger_engine() after resetting the busy flag.
676 *
677 * - Driver-level IORBs are not synchronized by adapter-level 'busy' flags.
678 * However, the driver-level queue is worked "one entry at a time" which
679 * means that no new IORBs will be queued on the driver-level queue until
680 * the head element has completed processing. This means that driver-
681 * level IORB handlers don't need to protect against each other. But they
682 * they do need to keep in mind interference with port-level IORBs:
683 *
684 * - Driver-level IORB handlers must obtain the spinlock and/or flag all
685 * adapters as 'busy' which are affected by the driver-level IORB
686 *
687 * - Driver-level IORB handlers must not access the hardware of a
688 * particular adapter if it's flagged as 'busy' by another IORB.
689 */
690int trigger_engine_1(void)
691{
692 IORBH _far *iorb;
693 IORBH _far *next;
694 int iorbs_sent = 0;
695 int a;
696 int p;
697
698 iorbs_sent = 0;
699
700 /* process driver-level IORBs */
701 if ((iorb = driver_queue.root) != NULL && !add_workspace(iorb)->processing) {
702 send_iorb(iorb);
703 iorbs_sent++;
704 }
705
706 /* process port-level IORBs */
707 for (a = 0; a < ad_info_cnt; a++) {
708 AD_INFO *ai = ad_infos + a;
709 if (ai->busy) {
710 /* adapter is busy; don't process any IORBs */
711 continue;
712 }
713 for (p = 0; p <= ai->port_max; p++) {
714 /* send all queued IORBs on this port */
715 next = NULL;
716 for (iorb = ai->ports[p].iorb_queue.root; iorb != NULL; iorb = next) {
717 next = iorb->pNxtIORB;
718 if (!add_workspace(iorb)->processing) {
719 send_iorb(iorb);
720 iorbs_sent++;
721 }
722 }
723 }
724 }
725
726 return(iorbs_sent);
727}
728
729/******************************************************************************
730 * Send a single IORB to the corresponding AHCI adapter/port. This is just a
731 * switch board for calling the corresponding iocc_*() handler function.
732 *
733 * NOTE: This function is expected to be called with the driver-level spinlock
734 * aquired. It will release it before calling any of the handler
735 * functions and re-aquire it when done.
736 */
737void send_iorb(IORBH _far *iorb)
738{
739 /* Mark IORB as "processing" before doing anything else. Once the IORB is
740 * marked as "processing", we can release the spinlock because subsequent
741 * invocations of trigger_engine() (e.g. at interrupt time) will ignore this
742 * IORB.
743 */
744 while (suspended) DevHelp_ProcBlock((ULONG)&send_iorb, 1, WAIT_IS_INTERRUPTABLE);
745 add_workspace(iorb)->processing = 1;
746 spin_unlock(drv_lock);
747
748 switch (iorb->CommandCode) {
749
750 case IOCC_CONFIGURATION:
751 iocc_configuration(iorb);
752 break;
753
754 case IOCC_DEVICE_CONTROL:
755 iocc_device_control(iorb);
756 break;
757
758 case IOCC_UNIT_CONTROL:
759 iocc_unit_control(iorb);
760 break;
761
762 case IOCC_GEOMETRY:
763 iocc_geometry(iorb);
764 break;
765
766 case IOCC_EXECUTE_IO:
767 iocc_execute_io(iorb);
768 break;
769
770 case IOCC_UNIT_STATUS:
771 iocc_unit_status(iorb);
772 break;
773
774 case IOCC_ADAPTER_PASSTHRU:
775 iocc_adapter_passthru(iorb);
776 break;
777
778 default:
779 /* unsupported call */
780 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
781 iorb_done(iorb);
782 break;
783 }
784
785 /* re-aquire spinlock before returning to trigger_engine() */
786 spin_lock(drv_lock);
787}
788
789/******************************************************************************
790 * Handle IOCC_CONFIGURATION requests.
791 */
792void iocc_configuration(IORBH _far *iorb)
793{
794 int a;
795
796 switch (iorb->CommandModifier) {
797
798 case IOCM_COMPLETE_INIT:
799 /* Complete initialization. From now on, we won't have to restore the BIOS
800 * configuration after each command and we're fully operational (i.e. will
801 * use interrupts, timers and context hooks instead of polling).
802 */
803 if (!init_complete) {
804 dprintf("leaving initialization mode\n");
805 for (a = 0; a < ad_info_cnt; a++) {
806 lock_adapter(ad_infos + a);
807 ahci_complete_init(ad_infos + a);
808 }
809 init_complete = 1;
810
811 /* DAZ turn off COM port output if on */
812 //com_base = 0;
813
814 /* release all adapters */
815 for (a = 0; a < ad_info_cnt; a++) {
816 unlock_adapter(ad_infos + a);
817 }
818
819 /* register APM hook */
820 apm_init();
821
822 if (!TRACE_ACTIVE) build_user_info();
823 }
824 iorb_done(iorb);
825 break;
826
827 case IOCM_GET_DEVICE_TABLE:
828 /* construct a device table */
829 iocm_device_table(iorb);
830 break;
831
832 default:
833 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
834 iorb_done(iorb);
835 break;
836 }
837}
838
839/******************************************************************************
840 * Handle IOCC_DEVICE_CONTROL requests.
841 */
842void iocc_device_control(IORBH _far *iorb)
843{
844 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
845 IORBH _far *ptr;
846 IORBH _far *next = NULL;
847 int p = iorb_unit_port(iorb);
848 int d = iorb_unit_device(iorb);
849
850 switch (iorb->CommandModifier) {
851
852 case IOCM_ABORT:
853 /* abort all pending commands on specified port and device */
854 spin_lock(drv_lock);
855 for (ptr = ai->ports[p].iorb_queue.root; ptr != NULL; ptr = next) {
856 next = ptr->pNxtIORB;
857 /* move all matching IORBs to the abort queue */
858 if (ptr != iorb && iorb_unit_device(ptr) == d) {
859 iorb_queue_del(&ai->ports[p].iorb_queue, ptr);
860 iorb_queue_add(&abort_queue, ptr);
861 ptr->ErrorCode = IOERR_CMD_ABORTED;
862 }
863 }
864 spin_unlock(drv_lock);
865
866 /* trigger reset context hook which will finish the abort processing */
867 DevHelp_ArmCtxHook(0, reset_ctxhook_h);
868 break;
869
870 case IOCM_SUSPEND:
871 case IOCM_RESUME:
872 case IOCM_GET_QUEUE_STATUS:
873 /* Suspend/resume operations allow access to the hardware for other
874 * entities such as IBMIDECD.FLT. Since os2ahci implements both ATA
875 * and ATAPI in the same driver, this won't be required.
876 */
877 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
878 break;
879
880 case IOCM_LOCK_MEDIA:
881 case IOCM_UNLOCK_MEDIA:
882 case IOCM_EJECT_MEDIA:
883 /* unit control commands to lock, unlock and eject media */
884 /* will be supported later... */
885 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
886 break;
887
888 default:
889 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
890 break;
891 }
892
893 iorb_done(iorb);
894}
895
896/******************************************************************************
897 * Handle IOCC_UNIT_CONTROL requests.
898 */
899void iocc_unit_control(IORBH _far *iorb)
900{
901 IORB_UNIT_CONTROL _far *iorb_uc = (IORB_UNIT_CONTROL _far *) iorb;
902 int a = iorb_unit_adapter(iorb);
903 int p = iorb_unit_port(iorb);
904 int d = iorb_unit_device(iorb);
905
906 spin_lock(drv_lock);
907 switch (iorb->CommandModifier) {
908
909 case IOCM_ALLOCATE_UNIT:
910 /* allocate unit for exclusive access */
911 if (ad_infos[a].ports[p].devs[d].allocated) {
912 iorb_seterr(iorb, IOERR_UNIT_ALLOCATED);
913 } else {
914 ad_infos[a].ports[p].devs[d].allocated = 1;
915 }
916 break;
917
918 case IOCM_DEALLOCATE_UNIT:
919 /* deallocate exclusive access to unit */
920 if (!ad_infos[a].ports[p].devs[d].allocated) {
921 iorb_seterr(iorb, IOERR_UNIT_NOT_ALLOCATED);
922 } else {
923 ad_infos[a].ports[p].devs[d].allocated = 0;
924 }
925 break;
926
927 case IOCM_CHANGE_UNITINFO:
928 /* Change unit (device) information. One reason for this IOCM is the
929 * interface for filter device drivers: a filter device driver can
930 * either change existing UNITINFOs or permanently allocate units
931 * and fabricate new [logical] units; the former is the reason why we
932 * must store the pointer to the updated UNITNIFO for subsequent
933 * IOCC_CONFIGURATION/IOCM_GET_DEVICE_TABLE calls.
934 */
935 if (!ad_infos[a].ports[p].devs[d].allocated) {
936 iorb_seterr(iorb, IOERR_UNIT_NOT_ALLOCATED);
937 break;
938 }
939 ad_infos[a].ports[p].devs[d].unit_info = iorb_uc->pUnitInfo;
940 break;
941
942 default:
943 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
944 break;
945 }
946
947 spin_unlock(drv_lock);
948 iorb_done(iorb);
949}
950
951/******************************************************************************
952 * Scan all ports for AHCI devices and construct a DASD device table.
953 *
954 * NOTES: This function may be called multiple times. Only the first
955 * invocation will actually scan for devices; all subsequent calls will
956 * merely return the results of the initial scan, potentially augmented
957 * by modified unit infos after IOCC_CONFIGURATION/IOCM_CHANGE_UNITINFO
958 * requests.
959 *
960 * In order to support applications that can't deal with ATAPI devices
961 * (i.e. need a SCSI adapter) os2ahci will optionally report ATAPI
962 * dvices as SCSI devices. The corresponding SCSI adapter doesn't
963 * really exist and is only reported here for the IOCM_GET_DEVICETABLE
964 * request. The units attached to this adapter will use the real HW
965 * unit IDs, thus we'll never receive a command specific to the
966 * emulated SCSI adapter and won't need to set up any sort of entity
967 * for it; the only purpose of the emulated SCSI adapter is to pass the
968 * bus type "AI_DEVBUS_SCSI_2" upstream, and the emulated units, of
969 * course. The emulated SCSI target IDs are allocated as follows:
970 *
971 * 0 the virtual adapter
972 * 1..n emulated devices; SCSI target ID increments sequentially
973 */
974void iocm_device_table(IORBH _far *iorb)
975{
976 IORB_CONFIGURATION _far *iorb_conf;
977 DEVICETABLE _far *dt;
978 char _far *pos;
979 int scsi_units = 0;
980 int scsi_id = 1;
981 int rc;
982 int dta;
983 int a;
984 int p;
985 int d;
986
987 iorb_conf = (IORB_CONFIGURATION _far *) iorb;
988 dt = iorb_conf->pDeviceTable;
989
990 spin_lock(drv_lock);
991
992 /* initialize device table header */
993 dt->ADDLevelMajor = ADD_LEVEL_MAJOR;
994 dt->ADDLevelMinor = ADD_LEVEL_MINOR;
995 dt->ADDHandle = add_handle;
996 dt->TotalAdapters = ad_info_cnt + 1;
997
998 /* set start of adapter and device information tables */
999 pos = (char _far *) (dt->pAdapter + dt->TotalAdapters);
1000
1001 /* go through all adapters, including the virtual SCSI adapter */
1002 for (dta = 0; dta < dt->TotalAdapters; dta++) {
1003 ADAPTERINFO _far *ptr = (ADAPTERINFO _far *) pos;
1004
1005 /* sanity check for sufficient space in device table */
1006 if ((u32) (ptr + 1) - (u32) dt > iorb_conf->DeviceTableLen) {
1007 dprintf("error: device table provided by DASD too small\n");
1008 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
1009 goto iocm_device_table_done;
1010 }
1011
1012 dt->pAdapter[dta] = (ADAPTERINFO _near *) ((u32) ptr & 0xffff);
1013 memset(ptr, 0x00, sizeof(*ptr));
1014
1015 ptr->AdapterIOAccess = AI_IOACCESS_BUS_MASTER;
1016 ptr->AdapterHostBus = AI_HOSTBUS_OTHER | AI_BUSWIDTH_32BIT;
1017 ptr->AdapterFlags = AF_16M | AF_HW_SCATGAT;
1018 ptr->MaxHWSGList = AHCI_MAX_SG / 2; /* AHCI S/G elements are 22 bits */
1019
1020 if (dta < ad_info_cnt) {
1021 /* this is a physical AHCI adapter */
1022 AD_INFO *ad_info = ad_infos + dta;
1023
1024 ptr->AdapterDevBus = AI_DEVBUS_ST506 | AI_DEVBUS_32BIT;
1025 sprintf(ptr->AdapterName, "AHCI_%d", dta);
1026
1027 if (!ad_info->port_scan_done) {
1028 /* first call; need to scan AHCI hardware for devices */
1029 if (ad_info->busy) {
1030 dprintf("error: port scan requested while adapter was busy\n");
1031 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
1032 goto iocm_device_table_done;
1033 }
1034 ad_info->busy = 1;
1035 spin_unlock(drv_lock);
1036 rc = ahci_scan_ports(ad_info);
1037 spin_lock(drv_lock);
1038 ad_info->busy = 0;
1039
1040 if (rc != 0) {
1041 dprintf("error: port scan failed on adapter #%d\n", dta);
1042 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
1043 goto iocm_device_table_done;
1044 }
1045 ad_info->port_scan_done = 1;
1046 }
1047
1048 /* insert physical (i.e. AHCI) devices into the device table */
1049 for (p = 0; p <= ad_info->port_max; p++) {
1050 for (d = 0; d <= ad_info->ports[p].dev_max; d++) {
1051 if (ad_info->ports[p].devs[d].present) {
1052 if (ad_info->ports[p].devs[d].atapi && emulate_scsi[dta][p]) {
1053 /* only report this unit as SCSI unit */
1054 scsi_units++;
1055 continue;
1056 }
1057 if (add_unit_info(iorb_conf, dta, dta, p, d, 0)) {
1058 goto iocm_device_table_done;
1059 }
1060 }
1061 }
1062 }
1063
1064 } else {
1065 /* this is the virtual SCSI adapter */
1066 if (scsi_units == 0) {
1067 /* not a single unit to be emulated via SCSI */
1068 dt->TotalAdapters--;
1069 break;
1070 }
1071
1072 /* set adapter name and bus type to mimic a SCSI controller */
1073 ptr->AdapterDevBus = AI_DEVBUS_SCSI_2 | AI_DEVBUS_16BIT;
1074 sprintf(ptr->AdapterName, "AHCI_SCSI_0");
1075
1076 /* add all ATAPI units to be emulated by this virtual adaper */
1077 for (a = 0; a < ad_info_cnt; a++) {
1078 AD_INFO *ad_info = ad_infos + a;
1079
1080 for (p = 0; p <= ad_info->port_max; p++) {
1081 for (d = 0; d <= ad_info->ports[p].dev_max; d++) {
1082 if (ad_info->ports[p].devs[d].present &&
1083 ad_info->ports[p].devs[d].atapi &&
1084 emulate_scsi[a][p]) {
1085 if (add_unit_info(iorb_conf, dta, a, p, d, scsi_id++)) {
1086 goto iocm_device_table_done;
1087 }
1088 }
1089 }
1090 }
1091 }
1092 }
1093
1094 /* calculate offset for next adapter */
1095 pos = (char _far *) (ptr->UnitInfo + ptr->AdapterUnits);
1096 }
1097
1098iocm_device_table_done:
1099 spin_unlock(drv_lock);
1100 iorb_done(iorb);
1101}
1102
1103/******************************************************************************
1104 * Handle IOCC_GEOMETRY requests.
1105 */
1106void iocc_geometry(IORBH _far *iorb)
1107{
1108 switch (iorb->CommandModifier) {
1109
1110 case IOCM_GET_MEDIA_GEOMETRY:
1111 case IOCM_GET_DEVICE_GEOMETRY:
1112 add_workspace(iorb)->idempotent = 1;
1113 ahci_get_geometry(iorb);
1114 break;
1115
1116 default:
1117 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
1118 iorb_done(iorb);
1119 }
1120}
1121
1122/******************************************************************************
1123 * Handle IOCC_EXECUTE_IO requests.
1124 */
1125void iocc_execute_io(IORBH _far *iorb)
1126{
1127 switch (iorb->CommandModifier) {
1128
1129 case IOCM_READ:
1130 add_workspace(iorb)->idempotent = 1;
1131 ahci_read(iorb);
1132 break;
1133
1134 case IOCM_READ_VERIFY:
1135 add_workspace(iorb)->idempotent = 1;
1136 ahci_verify(iorb);
1137 break;
1138
1139 case IOCM_WRITE:
1140 add_workspace(iorb)->idempotent = 1;
1141 ahci_write(iorb);
1142 break;
1143
1144 case IOCM_WRITE_VERIFY:
1145 add_workspace(iorb)->idempotent = 1;
1146 ahci_write(iorb);
1147 break;
1148
1149 default:
1150 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
1151 iorb_done(iorb);
1152 }
1153}
1154
1155/******************************************************************************
1156 * Handle IOCC_UNIT_STATUS requests.
1157 */
1158void iocc_unit_status(IORBH _far *iorb)
1159{
1160 switch (iorb->CommandModifier) {
1161
1162 case IOCM_GET_UNIT_STATUS:
1163 add_workspace(iorb)->idempotent = 1;
1164 ahci_unit_ready(iorb);
1165 break;
1166
1167 default:
1168 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
1169 iorb_done(iorb);
1170 }
1171}
1172
1173/******************************************************************************
1174 * Handle IOCC_ADAPTER_PASSTHROUGH requests.
1175 */
1176void iocc_adapter_passthru(IORBH _far *iorb)
1177{
1178 switch (iorb->CommandModifier) {
1179
1180 case IOCM_EXECUTE_CDB:
1181 add_workspace(iorb)->idempotent = 0;
1182 ahci_execute_cdb(iorb);
1183 break;
1184
1185 case IOCM_EXECUTE_ATA:
1186 add_workspace(iorb)->idempotent = 0;
1187 ahci_execute_ata(iorb);
1188 break;
1189
1190 default:
1191 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
1192 iorb_done(iorb);
1193 }
1194}
1195
1196/******************************************************************************
1197 * Add an IORB to the specified queue. This function must be called with the
1198 * adapter-level spinlock aquired.
1199 */
1200void iorb_queue_add(IORB_QUEUE _far *queue, IORBH _far *iorb)
1201{
1202 if (iorb_priority(iorb) {
1203 /* priority IORB; insert at first position */
1204 iorb->pNxtIORB = queue->root;
1205 queue->root = iorb;
1206
1207 } else {
1208 /* append IORB to end of queue */
1209 iorb->pNxtIORB = NULL;
1210
1211 if (queue->root == NULL) {
1212 queue->root = iorb;
1213 } else {
1214 queue->tail->pNxtIORB = iorb;
1215 }
1216 queue->tail = iorb;
1217 }
1218
1219 if (debug) {
1220 /* determine queue type (local, driver, abort or port) and minimum debug
1221 * level; otherwise, queue debug prints can become really confusing.
1222 */
1223 char *queue_type;
1224 int min_debug = 1;
1225
1226 if ((u32) queue >> 16 == (u32) (void _far *) &queue >> 16) {
1227 /* this queue is on the stack */
1228 queue_type = "local";
1229 min_debug = 2;
1230
1231 } else if (queue == &driver_queue) {
1232 queue_type = "driver";
1233
1234 } else if (queue == &abort_queue) {
1235 queue_type = "abort";
1236 min_debug = 2;
1237
1238 } else {
1239 queue_type = "port";
1240 }
1241
1242 if (debug > min_debug) {
1243 aprintf("IORB %Fp queued (cmd = %d/%d, queue = %Fp [%s], timeout = %ld)\n",
1244 iorb, iorb->CommandCode, iorb->CommandModifier, queue, queue_type,
1245 iorb->Timeout);
1246 }
1247 }
1248}
1249
1250/******************************************************************************
1251 * Remove an IORB from the specified queue. This function must be called with
1252 * the adapter-level spinlock aquired.
1253 */
1254int iorb_queue_del(IORB_QUEUE _far *queue, IORBH _far *iorb)
1255{
1256 IORBH _far *_iorb;
1257 IORBH _far *_prev = NULL;
1258 int found = 0;
1259
1260 for (_iorb = queue->root; _iorb != NULL; _iorb = _iorb->pNxtIORB) {
1261 if (_iorb == iorb) {
1262 /* found the IORB to be removed */
1263 if (_prev != NULL) {
1264 _prev->pNxtIORB = _iorb->pNxtIORB;
1265 } else {
1266 queue->root = _iorb->pNxtIORB;
1267 }
1268 if (_iorb == queue->tail) {
1269 queue->tail = _prev;
1270 }
1271 found = 1;
1272 break;
1273 }
1274 _prev = _iorb;
1275 }
1276
1277 if (found) {
1278 ddprintf("IORB %Fp removed (queue = %Fp)\n", iorb, queue);
1279 } else {
1280 dprintf("IORB %Fp not found in queue %Fp\n", iorb, queue);
1281 }
1282
1283 return(!found);
1284}
1285
1286/******************************************************************************
1287 * Set the error code in the specified IORB
1288 *
1289 * NOTE: This function does *not* call iorb_done(). It merely sets the IORB
1290 * status to the specified error code.
1291 */
1292void iorb_seterr(IORBH _far *iorb, USHORT error_code)
1293{
1294 iorb->ErrorCode = error_code;
1295 iorb->Status |= IORB_ERROR;
1296}
1297
1298/******************************************************************************
1299 * Mark the specified IORB as done and notify the asynchronous post function,
1300 * if any. The IORB is also removed from the corresponding IORB queue.
1301 *
1302 * NOTES: This function does not clear the Status field; it merely adds the
1303 * IORB_DONE flag.
1304 *
1305 * This function is expected to be called *without* the corresponding
1306 * driver-level drv_lock aquired. It will aquire the spinlock before
1307 * updating the IORB queue and release it before notifying the upstream
1308 * code in order to prevent deadlocks.
1309 *
1310 * Due to this logic, this function is only good for simple task-time
1311 * completions. Functions working on lists of IORBs (such as interrupt
1312 * handlers or context hooks) should call iorb_complete() directly and
1313 * implement their own logic for removing the IORB from the port queue.
1314 * See abort_ctxhook() for an example.
1315 */
1316void iorb_done(IORBH _far *iorb)
1317{
1318 int a = iorb_unit_adapter(iorb);
1319 int p = iorb_unit_port(iorb);
1320
1321 /* remove IORB from corresponding queue */
1322 spin_lock(drv_lock);
1323 if (iorb_driver_level(iorb)) {
1324 iorb_queue_del(&driver_queue, iorb);
1325 } else {
1326 iorb_queue_del(&ad_infos[a].ports[p].iorb_queue, iorb);
1327 }
1328 aws_free(add_workspace(iorb));
1329 spin_unlock(drv_lock);
1330
1331 iorb_complete(iorb);
1332}
1333
1334/******************************************************************************
1335 * Complete an IORB. This should be called without the adapter-level spinlock
1336 * to allow the IORB completion routine to perform whatever processing it
1337 * requires. This implies that the IORB should no longer be in any global
1338 * queue because the IORB completion routine may well reuse the IORB and send
1339 * the next request to us before even returning from this function.
1340 */
1341void iorb_complete(IORBH _far *iorb)
1342{
1343 iorb->Status |= IORB_DONE;
1344
1345 ddprintf("IORB %Fp complete (status = 0x%04x, error = 0x%04x)\n",
1346 iorb, iorb->Status, iorb->ErrorCode);
1347
1348 if (iorb->RequestControl & IORB_ASYNC_POST) {
1349 iorb->NotifyAddress(iorb);
1350 }
1351}
1352
1353/******************************************************************************
1354 * Requeue the specified IORB such that it will be sent downstream for
1355 * processing again. This includes freeing all resources currently allocated
1356 * (timer, buffer, ...) and resetting the flags to 0. The driver-level
1357 * spinlock must be aquired when calling this function.
1358 *
1359 * The following flags are preserved:
1360 * - no_ncq
1361 */
1362void iorb_requeue(IORBH _far *iorb)
1363{
1364 ADD_WORKSPACE _far *aws = add_workspace(iorb);
1365 u16 no_ncq = aws->no_ncq;
1366 u16 unaligned = aws->unaligned;
1367 u16 retries = aws->retries;
1368
1369 aws_free(aws);
1370 memset(aws, 0x00, sizeof(*aws));
1371
1372 aws->no_ncq = no_ncq;
1373 aws->unaligned = unaligned;
1374 aws->retries = retries;
1375}
1376
1377/******************************************************************************
1378 * Free resources in ADD workspace (timer, buffer, ...). This function should
1379 * be called with the spinlock held to prevent race conditions.
1380 */
1381void aws_free(ADD_WORKSPACE _far *aws)
1382{
1383 if (aws->timer != 0) {
1384 ADD_CancelTimer(aws->timer);
1385 aws->timer = 0;
1386 }
1387
1388 if (aws->buf != NULL) {
1389 free(aws->buf);
1390 aws->buf = NULL;
1391 }
1392}
1393
1394/******************************************************************************
1395 * Lock the adapter, waiting for availability if necessary. This is expected
1396 * to be called at task/request time without the driver-level spinlock
1397 * aquired. Don't call at interrupt time.
1398 */
1399void lock_adapter(AD_INFO *ai)
1400{
1401 TIMER Timer;
1402
1403 spin_lock(drv_lock);
1404 while (ai->busy) {
1405 spin_unlock(drv_lock);
1406 timer_init(&Timer, 250);
1407 while (!timer_check_and_block(&Timer));
1408 spin_lock(drv_lock);
1409 }
1410 ai->busy = 1;
1411 spin_unlock(drv_lock);
1412}
1413
1414/******************************************************************************
1415 * Unlock adapter (i.e. reset busy flag)
1416 */
1417void unlock_adapter(AD_INFO *ai)
1418{
1419 ai->busy = 0;
1420}
1421
1422/******************************************************************************
1423 * Timeout handler for I/O commands. Since timeout handling can involve
1424 * lengthy operations like port resets, the main code is located in a
1425 * separate function which is invoked via a context hook.
1426 */
1427void _cdecl _far timeout_callback(ULONG timer_handle, ULONG p1,
1428 ULONG p2)
1429{
1430 IORBH _far *iorb = (IORBH _far *) p1;
1431 int a = iorb_unit_adapter(iorb);
1432 int p = iorb_unit_port(iorb);
1433
1434 ADD_CancelTimer(timer_handle);
1435 dprintf("timeout for IORB %Fp\n", iorb);
1436
1437 /* Move the timed-out IORB to the abort queue. Since it's possible that the
1438 * IORB has completed after the timeout has expired but before we got to
1439 * this line of code, we'll check the return code of iorb_queue_del(): If it
1440 * returns an error, the IORB must have completed a few microseconds ago and
1441 * there is no timeout.
1442 */
1443 spin_lock(drv_lock);
1444 if (iorb_queue_del(&ad_infos[a].ports[p].iorb_queue, iorb) == 0) {
1445 iorb_queue_add(&abort_queue, iorb);
1446 iorb->ErrorCode = IOERR_ADAPTER_TIMEOUT;
1447 }
1448 spin_unlock(drv_lock);
1449
1450 /* Trigger abort processing function. We don't really care whether this
1451 * succeeds because the only reason why it would fail should be multiple
1452 * calls to DevHelp_ArmCtxHook() before the context hook had a chance to
1453 * start executing, which leaves two scenarios:
1454 *
1455 * - We succeded in arming the context hook. Fine.
1456 *
1457 * - We armed the context hook a second time before it had a chance to
1458 * start executing. In this case, the already scheduled context hook
1459 * will process our IORB as well.
1460 */
1461 DevHelp_ArmCtxHook(0, reset_ctxhook_h);
1462
1463 /* Set up a watchdog timer which calls the context hook manually in case
1464 * some kernel thread is looping around the IORB_COMPLETE status bit
1465 * without yielding the CPU (kernel threads don't preempt). This shouldn't
1466 * happen per design because kernel threads are supposed to yield but it
1467 * does in the early boot phase.
1468 */
1469 ADD_StartTimerMS(&th_reset_watchdog, 5000, (PFN) reset_watchdog, 0, 0);
1470}
1471
1472/******************************************************************************
1473 * Reset handler watchdog. If a timeout occurs, a context hook is armed which
1474 * will execute as soon as a kernel thread yields the CPU. However, some
1475 * kernel components won't yield the CPU during the early boot phase and the
1476 * only way to kick some sense into those components is to run the context
1477 * hook right inside this timer callback. Not exactly pretty, especially
1478 * considering the fact that context hooks were implemented to prevent running
1479 * lengthy operations like a port reset at interrupt time, but without this
1480 * watchdog mechanism we run the risk of getting completely stalled by device
1481 * problems during the early boot phase.
1482 */
1483void _cdecl _far reset_watchdog(ULONG timer_handle, ULONG p1,
1484 ULONG p2)
1485{
1486 /* reset watchdog timer */
1487 ADD_CancelTimer(timer_handle);
1488 dprintf("reset watchdog invoked\n");
1489
1490 /* call context hook manually */
1491 reset_ctxhook(0);
1492}
1493
1494/******************************************************************************
1495 * small_code_ - this dummy func resolves the undefined reference linker
1496 * error that occurrs when linking WATCOM objects with DDK's link.exe
1497 */
1498void _cdecl small_code_(void)
1499{
1500}
1501
1502/******************************************************************************
1503 * Add unit info to ADAPTERINFO array (IOCC_GET_DEVICE_TABLE requests). The
1504 * adapter info array in the device table, dt->pAdapter[], is expected to be
1505 * initialized for the specified index (dt_ai).
1506 *
1507 * Please note that the device table adapter index, dta, is not always equal
1508 * to the physical adapter index, a: if SCSI emulation has been activated, the
1509 * last reported adapter is a virtual SCSI adapter and the physical adapter
1510 * indexes for those units are, of course, different from the device table
1511 * index of the virtual SCSI adapter.
1512 */
1513static int add_unit_info(IORB_CONFIGURATION _far *iorb_conf, int dta,
1514 int a, int p, int d, int scsi_id)
1515{
1516 DEVICETABLE _far *dt = iorb_conf->pDeviceTable;
1517 ADAPTERINFO _far *ptr = (ADAPTERINFO _far *) (((u32) dt & 0xffff0000U) +
1518 (u16) dt->pAdapter[dta]);
1519 UNITINFO _far *ui = ptr->UnitInfo + ptr->AdapterUnits;
1520 AD_INFO *ai = ad_infos + a;
1521
1522 if ((u32) (ui + 1) - (u32) dt > iorb_conf->DeviceTableLen) {
1523 dprintf("error: device table provided by DASD too small\n");
1524 iorb_seterr(&iorb_conf->iorbh, IOERR_CMD_SW_RESOURCE);
1525 return(-1);
1526 }
1527
1528 if (ai->ports[p].devs[d].unit_info == NULL) {
1529 /* provide original information about this device (unit) */
1530 memset(ui, 0x00, sizeof(*ui));
1531 ui->AdapterIndex = dta; /* device table adapter index */
1532 ui->UnitHandle = iorb_unit(a, p, d); /* physical adapter index */
1533 ui->UnitIndex = ptr->AdapterUnits;
1534 ui->UnitType = ai->ports[p].devs[d].dev_type;
1535 ui->QueuingCount = ai->ports[p].devs[d].ncq_max;;
1536 if (ai->ports[p].devs[d].removable) {
1537 ui->UnitFlags |= UF_REMOVABLE;
1538 }
1539 if (scsi_id > 0) {
1540 /* set fake SCSI ID for this unit */
1541 ui->UnitSCSITargetID = scsi_id;
1542 }
1543 } else {
1544 /* copy updated device (unit) information (IOCM_CHANGE_UNITINFO) */
1545 memcpy(ui, ai->ports[p].devs[d].unit_info, sizeof(*ui));
1546 }
1547
1548 ptr->AdapterUnits++;
1549 return(0);
1550}
1551
1552/*******************************************************************************
1553 * Register kernel exit handler for trap dumps. Our exit handler will be called
1554 * right before the kernel starts a dump; that's where we reset the controller
1555 * so it supports BIOS int13 I/O calls.
1556 */
1557static void register_krnl_exit(void)
1558{
1559 _asm {
1560 push ds
1561 push es
1562 push bx
1563 push si
1564 push di
1565
1566 mov ax, FLAG_KRNL_EXIT_ADD
1567 mov cx, TYPE_KRNL_EXIT_INT13
1568 mov bx, SEG asm_krnl_exit
1569 mov si, OFFSET asm_krnl_exit
1570 mov dl, DevHlp_RegisterKrnlExit
1571
1572 call dword ptr [Device_Help]
1573
1574 pop di
1575 pop si
1576 pop bx
1577 pop es
1578 pop ds
1579 }
1580
1581 dprintf("Registered kernel exit routine for INT13 mode\n");
1582}
1583
Note: See TracBrowser for help on using the repository browser.