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

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

Changed bldlevel handling

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