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

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

Fix spin-up / power-up issue on some hardware
Changes to debug output
Fixup makefiles

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