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

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

Increased size of debug buffer

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