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

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

Suspend/resume changes

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