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

Last change on this file since 147 was 147, checked in by rousseau, 13 years ago

Changed the default value of the /r option (TRACK ticket #21)

Reason for the change

o Snip from the TRAC ticket

<snip>

One thing that keeps coming up in some cases is that on systems with an
Intel disc controller you need the /R option specified.
In some cases the system is able to boot with from the CD but it does
not recognize the hard disc.
Using the /R option fixes.

</snip>

o Logic suggests...

Since /r was already turned on when an Intel AHCI controller was detected
to be in SATA mode, this report seems to imply that for some obscure reason
a port reset is needed even when the BIOS programmed the controller in
AHCI mode. This would suggest some faulty BIOS initialization.

Changed behavior

o The /r option is now on by default

It can be turned off by specifying /!r on the driver command-line.
However, when the [Intel] AHCI controller was found to be initialized
by the BIOS in SATA mode, ports will always be reset, even when /!r
is specified. This seems to be an [Intel] AHCI controller requirement.

Comment reference

o From a comment in ahci.c around line 146

<snip>

Adapter is not in AHCI mode and the spec says a COMRESET is
required when switching from SATA to AHCI mode and vice versa.

</snip>

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