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

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

debugging updates

File size: 51.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 "ioctl.h"
29#include "version.h"
30
31/* -------------------------- macros and constants ------------------------- */
32
33/* parse integer command line parameter */
34#define drv_parm_int(s, value, type, radix) \
35 { \
36 char _far *_ep; \
37 if ((s)[1] != ':') { \
38 cprintf("%s: missing colon (:) after /%c\n", drv_name, *(s)); \
39 goto init_fail; \
40 } \
41 value = (type) strtol((s) + 2, \
42 (const char _far* _far*) &_ep, \
43 radix); \
44 s = _ep; \
45 }
46
47/* set two-dimensional array of port options */
48#define set_port_option(opt, val) \
49 if (adapter_index == -1) { \
50 /* set option for all adapters and ports */ \
51 memset(opt, val, sizeof(opt)); \
52 } else if (port_index == -1) { \
53 /* set option for all ports on current adapter */ \
54 memset(opt[adapter_index], val, sizeof(*opt)); \
55 } else { \
56 /* set option for specific port */ \
57 opt[adapter_index][port_index] = val; \
58 }
59
60/* constants for undefined kernel exit routine;
61 * see register_krnl_exit() func */
62#define DevHlp_RegisterKrnlExit 0x006f
63
64#define FLAG_KRNL_EXIT_ADD 0x1000
65#define FLAG_KRNL_EXIT_REMOVE 0x2000
66
67#define TYPE_KRNL_EXIT_NMI 0x0000 /* non masked interrupts */
68#define TYPE_KRNL_EXIT_SFF 0x0001 /* system fatal faults */
69#define TYPE_KRNL_EXIT_PROCDUMP 0x0002
70#define TYPE_KRNL_EXIT_DYN 0x0003
71#define TYPE_KRNL_EXIT_INT13 0x0004 /* enable int13 IO */
72
73/* ------------------------ typedefs and structures ------------------------ */
74
75/* -------------------------- function prototypes -------------------------- */
76
77void _cdecl small_code_ (void);
78
79static int add_unit_info (IORB_CONFIGURATION _far *iorb_conf, int dt_ai,
80 int a, int p, int d, int scsi_id);
81
82static void register_krnl_exit (void);
83
84/* ------------------------ global/static variables ------------------------ */
85
86int debug = 0; /* if > 0, print debug messages to COM1 */
87int thorough_scan = 1; /* if != 0, perform thorough PCI scan */
88int init_reset = 1; /* if != 0, reset ports during init */
89int force_write_cache; /* if != 0, force write cache */
90int verbosity = 1; /* == 1 -> show sign on banner
91 * > 1 -> show adapter info during boot */
92int use_lvm_info = 1;
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 DVENDOR, /* 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 */
124int suspended;
125
126/* apapter/port-specific options saved when parsing the command line */
127u8 emulate_scsi[MAX_AD][AHCI_MAX_PORTS];
128u8 enable_ncq[MAX_AD][AHCI_MAX_PORTS];
129u8 link_speed[MAX_AD][AHCI_MAX_PORTS];
130u8 link_power[MAX_AD][AHCI_MAX_PORTS];
131u8 track_size[MAX_AD][AHCI_MAX_PORTS];
132
133static char init_msg[] = "%s driver version %d.%02d\n";
134static char exit_msg[] = "%s driver *not* installed\n";
135char BldLevel[] = BLDLEVEL;
136
137/* ----------------------------- start of code ----------------------------- */
138
139/******************************************************************************
140 * OS/2 device driver main strategy function. This function is only used
141 * for initialization purposes; all other calls go directly to the adapter
142 * device driver's strategy function.
143 *
144 * NOTE: this is also used as the IDC entry point. We expect an IOCTL request
145 * packet for IDC calls, so they can be handled by gen_ioctl.
146 */
147USHORT _cdecl c_strat(RPH _far *req)
148{
149 u16 rc;
150
151 switch (req->Cmd) {
152
153 case CMDInitBase:
154 rc = init_drv((RPINITIN _far *) req);
155 break;
156
157 case CMDShutdown:
158 rc = exit_drv(((RPSAVERESTORE _far *) req)->FuncCode);
159 break;
160
161 case CMDGenIOCTL:
162 rc = gen_ioctl((RP_GENIOCTL _far *) req);
163 break;
164
165 case CMDINPUT:
166 rc = char_dev_input((RP_RWV _far *) req);
167 break;
168
169 case CMDSaveRestore:
170 rc = sr_drv(((RPSAVERESTORE _far *) req)->FuncCode);
171 break;
172
173 default:
174 rc = STDON | STATUS_ERR_UNKCMD;
175 break;
176 }
177
178 return(rc);
179}
180
181/******************************************************************************
182 * Intialize the os2ahci driver. This includes command line parsing, scanning
183 * the PCI bus for supported AHCI adapters, etc.
184 */
185USHORT init_drv(RPINITIN _far *req)
186{
187 static int init_drv_called;
188 static int init_drv_failed;
189 RPINITOUT _far *rsp = (RPINITOUT _far *) req;
190 DDD_PARM_LIST _far *ddd_pl = (DDD_PARM_LIST _far *) req->InitArgs;
191 APIRET rmrc;
192 char _far *cmd_line;
193 char _far *s;
194 int adapter_index = -1;
195 int port_index = -1;
196 int invert_option;
197 int optval;
198 u16 vendor;
199 u16 device;
200
201 if (init_drv_called) {
202 /* This is the init call for the second (legacy IBMS506$) character
203 * device driver. If the main driver failed initialization, fail this
204 * one as well.
205 */
206 rsp->CodeEnd = (u16) end_of_code;
207 rsp->DataEnd = (u16) &end_of_data;
208 return(STDON | ((init_drv_failed) ? ERROR_I24_QUIET_INIT_FAIL : 0));
209 }
210 init_drv_called = 1;
211 suspended = 0;
212
213 /* set device helper entry point */
214 Device_Help = req->DevHlpEP;
215
216 /* create driver-level spinlock */
217 DevHelp_CreateSpinLock(&drv_lock);
218
219 /* initialize libc code */
220 init_libc();
221
222 /* register driver with resource manager */
223 if ((rmrc = RMCreateDriver(&rm_drvinfo, &rm_drvh)) != RMRC_SUCCESS) {
224 cprintf("%s: failed to register driver with resource manager (rc = %d)\n",
225 drv_name, rmrc);
226 goto init_fail;
227 }
228
229 /* parse command line parameters */
230 cmd_line = (char _far *) ((u32) ddd_pl & 0xffff0000l) + ddd_pl->cmd_line_args;
231
232 for (s = cmd_line; *s != 0; s++) {
233 if (*s == '/') {
234 if ((invert_option = (s[1] == '!')) != 0) {
235 s++;
236 }
237 s++;
238 switch (tolower(*s)) {
239
240 case '\0':
241 /* end of command line; can only happen if command line is incorrect */
242 cprintf("%s: incomplete command line option\n", drv_name);
243 goto init_fail;
244
245 case 'c':
246 /* set COM port base address for debug messages */
247 drv_parm_int(s, com_base, u16, 16);
248 if (com_base == 1) com_base = 0x3f8;
249 if (com_base == 2) com_base = 0x2f8;
250 break;
251
252 case 'd':
253 /* increase debug level */
254 debug++;
255 break;
256
257 case 'g':
258 /* add specfied PCI ID as a supported generic AHCI adapter */
259 drv_parm_int(s, vendor, u16, 16);
260 s--;
261 drv_parm_int(s, device, u16, 16);
262 if (add_pci_id(vendor, device)) {
263 cprintf("%s: failed to add PCI ID %04x:%04x\n", drv_name, vendor, device);
264 goto init_fail;
265 }
266 thorough_scan = 1;
267 break;
268
269 case 't':
270 /* perform thorough PCI scan (i.e. look for individual supported PCI IDs) */
271 thorough_scan = !invert_option;
272 break;
273
274 case 'r':
275 /* reset ports during initialization */
276 init_reset = !invert_option;
277 break;
278
279 case 'f':
280 /* force write cache regardless of IORB flags */
281 force_write_cache = 1;
282 break;
283
284 case 'a':
285 /* set adapter index for adapter and port-related options */
286 drv_parm_int(s, adapter_index, int, 10);
287 if (adapter_index < 0 || adapter_index >= MAX_AD) {
288 cprintf("%s: invalid adapter index (%d)\n", drv_name, adapter_index);
289 goto init_fail;
290 }
291 break;
292
293 case 'p':
294 /* set port index for port-related options */
295 drv_parm_int(s, port_index, int, 10);
296 if (port_index < 0 || port_index >= AHCI_MAX_PORTS) {
297 cprintf("%s: invalid port index (%d)\n", drv_name, port_index);
298 goto init_fail;
299 }
300 break;
301
302 case 'i':
303 /* ignore current adapter index */
304 if (adapter_index >= 0) {
305 ad_ignore |= 1U << adapter_index;
306 }
307 break;
308
309 case 's':
310 /* enable SCSI emulation for ATAPI devices */
311 set_port_option(emulate_scsi, !invert_option);
312 break;
313
314 case 'n':
315 /* enable NCQ */
316 set_port_option(enable_ncq, !invert_option);
317 break;
318
319 case 'l':
320 /* set link speed or power savings */
321 s++;
322 switch (tolower(*s)) {
323 case 's':
324 /* set link speed */
325 drv_parm_int(s, optval, int, 10);
326 set_port_option(link_speed, optval);
327 break;
328 case 'p':
329 /* set power management */
330 drv_parm_int(s, optval, int, 10);
331 set_port_option(link_power, optval);
332 break;
333 default:
334 cprintf("%s: invalid link parameter (%c)\n", drv_name, *s);
335 goto init_fail;
336 }
337 /* need to reset the port in order to establish link settings */
338 init_reset = 1;
339 break;
340
341 case '4':
342 /* enable 4K sector geometry enhancement (track size = 56) */
343 if (!invert_option) {
344 set_port_option(track_size, 56);
345 }
346 break;
347
348 case 'z':
349 use_lvm_info = !invert_option;
350 break;
351
352 case 'v':
353 /* be verbose during boot */
354 verbosity++;
355 break;
356
357 case 'q':
358 /* be quiet */
359 verbosity = -1000;
360 break;
361
362 default:
363 cprintf("%s: unknown option: /%c\n", drv_name, *s);
364 goto init_fail;
365 }
366 }
367 }
368
369 /* print initialization message */
370 ciprintf(init_msg, drv_name, VERSION / 100, VERSION % 100);
371
372//#ifdef ECS_BUILD
373// ciprintf("This driver is licensed for use only in conjunction with eComStation.\n");
374//#else
375// ciprintf("This is the Open Watcom build.\n");
376//#endif
377
378 if (debug) {
379 /* initialize com port for debug output */
380 //DAZ init_com();
381 }
382
383 /* initialize trace buffer if applicable */
384 if (TRACE_ACTIVE) {
385 /* debug is on, but COM port is off -> use our trace buffer */
386 trace_init(AHCI_TRACE_BUF_SIZE);
387 } else {
388 trace_init(AHCI_INFO_BUF_SIZE);
389 }
390
391 printf_nts("BldLevel: %s\n", BldLevel);
392 printf_nts("CmdLine: %Fs\n", cmd_line);
393
394 /* scan PCI bus for supported devices */
395 scan_pci_bus();
396
397 if (ad_info_cnt > 0) {
398 /* initialization succeeded and we found at least one AHCI adapter */
399 ADD_InitTimer(timer_pool, sizeof(timer_pool));
400 mdelay_cal();
401
402 if (DevHelp_RegisterDeviceClass(drv_name, (PFN) add_entry, 0, 1, &add_handle)) {
403 cprintf("%s: couldn't register device class\n", drv_name);
404 goto init_fail;
405 }
406
407 /* allocate context hooks */
408 if (DevHelp_AllocateCtxHook(mk_NPFN(restart_hook), &restart_ctxhook_h) != 0 ||
409 DevHelp_AllocateCtxHook(mk_NPFN(reset_hook), &reset_ctxhook_h) != 0 ||
410 DevHelp_AllocateCtxHook(mk_NPFN(engine_hook), &engine_ctxhook_h)) {
411 cprintf("%s: failed to allocate task-time context hooks\n", drv_name);
412 goto init_fail;
413 }
414
415 rsp->CodeEnd = (u16) end_of_code;
416 rsp->DataEnd = (u16) &end_of_data;
417
418 /* register kernel exit routine for trap dumps */
419 register_krnl_exit();
420
421 return(STDON);
422
423 } else {
424 /* no adapters found */
425 ciprintf(" No adapters found.\n");
426 }
427
428init_fail:
429 /* initialization failed; set segment sizes to 0 and return error */
430 rsp->CodeEnd = 0;
431 rsp->DataEnd = 0;
432 init_drv_failed = 1;
433
434 /* free context hooks */
435 if (engine_ctxhook_h != 0) DevHelp_FreeCtxHook(engine_ctxhook_h);
436 if (reset_ctxhook_h != 0) DevHelp_FreeCtxHook(reset_ctxhook_h);
437 if (restart_ctxhook_h != 0) DevHelp_FreeCtxHook(restart_ctxhook_h);
438
439 if (rm_drvh != 0) {
440 /* remove driver from resource manager */
441 RMDestroyDriver(rm_drvh);
442 }
443
444 ciprintf(exit_msg, drv_name);
445 return(STDON | ERROR_I24_QUIET_INIT_FAIL);
446}
447
448/******************************************************************************
449 * Generic IOCTL via character device driver. IOCTLs are used to control the
450 * driver operation and to execute native ATA and ATAPI (SCSI) commands from
451 * ring 3 applications. On top of that, some predefined IOCTLs (e.g. SMART
452 * commands for ATA disks) are implemented here.
453 */
454USHORT gen_ioctl(RP_GENIOCTL _far *ioctl)
455{
456 dprintf("IOCTL 0x%x/0x%x\n", (u16) ioctl->Category, (u16) ioctl->Function);
457
458 switch (ioctl->Category) {
459
460 case OS2AHCI_IOCTL_CATEGORY:
461 switch (ioctl->Function) {
462
463 case OS2AHCI_IOCTL_GET_DEVLIST:
464 return(ioctl_get_devlist(ioctl));
465
466 case OS2AHCI_IOCTL_PASSTHROUGH:
467 return(ioctl_passthrough(ioctl));
468
469 }
470 break;
471
472 case OS2AHCI_IDC_CATEGORY:
473 switch (ioctl->Function) {
474
475 case OS2AHCI_IDC_BIOSMODE:
476 /* reconfigure adapters in BIOS/int13 mode; needed for generating
477 * trap dumps on some machines. This was intended to be called by ACPI.PSD,
478 * but that is never done. This is obslete. The kernel exit accomplishes
479 * this instead.
480 *
481 * To enter BIOS mode, we flush all write caches, turn off interrupts
482 * and restore the BIOS configuration. This is exactly what
483 * apm_suspend() does.
484 */
485 apm_suspend();
486 return(STDON);
487
488 case OS2AHCI_IDC_BEEP:
489 /* IOCTL for IDC testing - just beep */
490 DevHelp_Beep(2000, 100);
491 return(STDON);
492 }
493 break;
494
495 case DSKSP_CAT_GENERIC:
496 return(ioctl_gen_dsk(ioctl));
497
498 case DSKSP_CAT_SMART:
499 return(ioctl_smart(ioctl));
500
501 }
502
503 return(STDON | STATUS_ERR_UNKCMD);
504}
505
506/******************************************************************************
507 * Read from character device. If tracing is on (internal ring buffer trace),
508 * we return data from the trace buffer; if not, we might return a device
509 * dump similar to IBM1S506.ADD/DANIS506.ADD (TODO).
510 */
511USHORT char_dev_input(RP_RWV _far *rwrb)
512{
513 return(trace_char_dev(rwrb));
514}
515
516/******************************************************************************
517 * Device driver exit handler. This handler is called when OS/2 shuts down and
518 * flushes the write caches of all attached devices. Since this is effectively
519 * the same we do when suspending, we'll call out to the corresponding APM
520 * function.
521 *
522 * NOTE: Errors are ignored because there's no way we could stop the shutdown
523 * or do something about the error, unless retrying endlessly is
524 * considered an option.
525 */
526USHORT exit_drv(int func)
527{
528 dprintf("exit_drv(%d) called\n", func);
529
530 if (func == 0) {
531 /* we're only interested in the second phase of the shutdown */
532 return(STDON);
533 }
534
535 apm_suspend();
536 return(STDON);
537}
538
539/******************************************************************************
540 * Device driver suspend/resume handler. This handler is called when ACPI is
541 * executing a suspend or resume.
542 */
543USHORT sr_drv(int func)
544{
545 dprintf("sr_drv(%d) called\n", func);
546
547 if (func) apm_resume();
548 else apm_suspend();
549
550 return(STDON);
551}
552
553/******************************************************************************
554 * ADD entry point. This is the main entry point for all ADD requests. Due to
555 * the asynchronous nature of ADD drivers, this function primarily queues the
556 * IORB(s) to the corresponding adapter or port queues, then triggers the
557 * state machine to initiate processing queued IORBs.
558 *
559 * NOTE: In order to prevent race conditions or engine stalls, certain rules
560 * around locking, unlocking and IORB handling in general have been
561 * established. Refer to the comments in "trigger_engine()" for
562 * details.
563 */
564void _cdecl _far _loadds add_entry(IORBH _far *first_iorb)
565{
566 IORBH _far *iorb;
567 IORBH _far *next = NULL;
568
569 spin_lock(drv_lock);
570
571 for (iorb = first_iorb; iorb != NULL; iorb = next) {
572 /* Queue this IORB. Queues primarily exist on port level but there are
573 * some requests which affect the whole driver, most notably
574 * IOCC_CONFIGURATION. In either case, adding the IORB to the driver or
575 * port queue will change the links, thus we need to save the original
576 * link in 'next'.
577 */
578 next = (iorb->RequestControl | IORB_CHAIN) ? iorb->pNxtIORB : 0;
579
580 iorb->Status = 0;
581 iorb->ErrorCode = 0;
582 memset(&iorb->ADDWorkSpace, 0x00, sizeof(ADD_WORKSPACE));
583
584 if (iorb_driver_level(iorb)) {
585 /* driver-level IORB */
586 iorb->UnitHandle = 0;
587 iorb_queue_add(&driver_queue, iorb);
588
589 } else {
590 /* port-level IORB */
591 int a = iorb_unit_adapter(iorb);
592 int p = iorb_unit_port(iorb);
593 int d = iorb_unit_device(iorb);
594
595 if (a >= ad_info_cnt ||
596 p > ad_infos[a].port_max ||
597 d > ad_infos[a].ports[p].dev_max ||
598 (ad_infos[a].port_map & (1UL << p)) == 0) {
599
600 /* unit handle outside of the allowed range */
601 dprintf("warning: IORB for %d.%d.%d out of range\n", a, p, d);
602 iorb->Status = IORB_ERROR;
603 iorb->ErrorCode = IOERR_CMD_SYNTAX;
604 iorb_complete(iorb);
605 continue;
606 }
607
608 iorb_queue_add(&ad_infos[a].ports[p].iorb_queue, iorb);
609 }
610 }
611
612 /* trigger state machine */
613 trigger_engine();
614
615 spin_unlock(drv_lock);
616}
617
618/******************************************************************************
619 * Trigger IORB queue engine. This is a wrapper function for trigger_engine_1()
620 * which will try to get all IORBs sent on their way a couple of times. If
621 * there are still IORBs ready for processing after this, this function will
622 * hand off to a context hook which will continue to trigger the engine until
623 * all IORBs have been sent.
624 *
625 * NOTE: While initialization has not completed (or during APM suspend/resume
626 * operations), this function will loop indefinitely because we can't
627 * rely on interrupt handlers or context hooks and complex IORBs
628 * requiring multiple requeues would eventually hang and time out if
629 * we stopped triggering here.
630 */
631void trigger_engine(void)
632{
633 int i;
634
635 for (i = 0; i < 3 || !init_complete; i++) {
636 if (trigger_engine_1() == 0) {
637 /* done -- all IORBs have been sent on their way */
638 return;
639 }
640 }
641
642 /* Something keeps bouncing; hand off to the engine context hook which will
643 * keep trying in the background.
644 */
645 DevHelp_ArmCtxHook(0, engine_ctxhook_h);
646}
647
648/******************************************************************************
649 * Trigger IORB queue engine in order to send commands in the driver/port IORB
650 * queues to the AHCI hardware. This function will return the number of IORBs
651 * sent. Keep in mind that IORBs might "bounce" if the adapter/port is not in
652 * a state to accept the command, thus it might take quite a few calls to get
653 * all IORBs on their way. This is why there's a wrapper function which tries
654 * it a few times, then hands off to a context hook which will keep trying in
655 * the background.
656 *
657 * IORBs might complete before send_iorb() has returned, at any time during
658 * interrupt processing or on another CPU on SMP systems. IORB completion
659 * means modifications to the corresponding IORB queue (the completed IORB
660 * is removed from the queue) thus we need to protect the IORB queues from
661 * race conditions. The safest approach short of keeping the driver-level
662 * spinlock aquired permanently is to keep it throughout this function and
663 * release it temporarily in send_iorb().
664 *
665 * This implies that the handler functions are fully responsible for aquiring
666 * the driver-level spinlock when they need it, and for releasing it again.
667 *
668 * As a rule of thumb, get the driver-level spinlock whenever accessing
669 * volatile variables (IORB queues, values in ad_info[], ...).
670 *
671 * Additional Notes:
672 *
673 * - This function is expected to be called with the spinlock aquired
674 *
675 * - Adapters can be flagged as 'busy' which means no new IORBs are sent (they
676 * just remain in the queue). This can be used to release the driver-level
677 * spinlock while making sure no new IORBs are going to hit the hardware.
678 * In order to prevent engine stalls, all handlers using this functionality
679 * need to invoke trigger_engine() after resetting the busy flag.
680 *
681 * - Driver-level IORBs are not synchronized by adapter-level 'busy' flags.
682 * However, the driver-level queue is worked "one entry at a time" which
683 * means that no new IORBs will be queued on the driver-level queue until
684 * the head element has completed processing. This means that driver-
685 * level IORB handlers don't need to protect against each other. But they
686 * they do need to keep in mind interference with port-level IORBs:
687 *
688 * - Driver-level IORB handlers must obtain the spinlock and/or flag all
689 * adapters as 'busy' which are affected by the driver-level IORB
690 *
691 * - Driver-level IORB handlers must not access the hardware of a
692 * particular adapter if it's flagged as 'busy' by another IORB.
693 */
694int trigger_engine_1(void)
695{
696 IORBH _far *iorb;
697 IORBH _far *next;
698 int iorbs_sent = 0;
699 int a;
700 int p;
701
702 iorbs_sent = 0;
703
704 /* process driver-level IORBs */
705 if ((iorb = driver_queue.root) != NULL && !add_workspace(iorb)->processing) {
706 send_iorb(iorb);
707 iorbs_sent++;
708 }
709
710 /* process port-level IORBs */
711 for (a = 0; a < ad_info_cnt; a++) {
712 AD_INFO *ai = ad_infos + a;
713 if (ai->busy) {
714 /* adapter is busy; don't process any IORBs */
715 continue;
716 }
717 for (p = 0; p <= ai->port_max; p++) {
718 /* send all queued IORBs on this port */
719 next = NULL;
720 for (iorb = ai->ports[p].iorb_queue.root; iorb != NULL; iorb = next) {
721 next = iorb->pNxtIORB;
722 if (!add_workspace(iorb)->processing) {
723 send_iorb(iorb);
724 iorbs_sent++;
725 }
726 }
727 }
728 }
729
730 return(iorbs_sent);
731}
732
733/******************************************************************************
734 * Send a single IORB to the corresponding AHCI adapter/port. This is just a
735 * switch board for calling the corresponding iocc_*() handler function.
736 *
737 * NOTE: This function is expected to be called with the driver-level spinlock
738 * aquired. It will release it before calling any of the handler
739 * functions and re-aquire it when done.
740 */
741void send_iorb(IORBH _far *iorb)
742{
743 /* Mark IORB as "processing" before doing anything else. Once the IORB is
744 * marked as "processing", we can release the spinlock because subsequent
745 * invocations of trigger_engine() (e.g. at interrupt time) will ignore this
746 * IORB.
747 */
748 add_workspace(iorb)->processing = 1;
749 spin_unlock(drv_lock);
750
751 switch (iorb->CommandCode) {
752
753 case IOCC_CONFIGURATION:
754 iocc_configuration(iorb);
755 break;
756
757 case IOCC_DEVICE_CONTROL:
758 iocc_device_control(iorb);
759 break;
760
761 case IOCC_UNIT_CONTROL:
762 iocc_unit_control(iorb);
763 break;
764
765 case IOCC_GEOMETRY:
766 iocc_geometry(iorb);
767 break;
768
769 case IOCC_EXECUTE_IO:
770 iocc_execute_io(iorb);
771 break;
772
773 case IOCC_UNIT_STATUS:
774 iocc_unit_status(iorb);
775 break;
776
777 case IOCC_ADAPTER_PASSTHRU:
778 iocc_adapter_passthru(iorb);
779 break;
780
781 default:
782 /* unsupported call */
783 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
784 iorb_done(iorb);
785 break;
786 }
787
788 /* re-aquire spinlock before returning to trigger_engine() */
789 spin_lock(drv_lock);
790}
791
792/******************************************************************************
793 * Handle IOCC_CONFIGURATION requests.
794 */
795void iocc_configuration(IORBH _far *iorb)
796{
797 int a;
798
799 switch (iorb->CommandModifier) {
800
801 case IOCM_COMPLETE_INIT:
802 /* Complete initialization. From now on, we won't have to restore the BIOS
803 * configuration after each command and we're fully operational (i.e. will
804 * use interrupts, timers and context hooks instead of polling).
805 */
806 if (!init_complete) {
807 dprintf("leaving initialization mode\n");
808 for (a = 0; a < ad_info_cnt; a++) {
809 lock_adapter(ad_infos + a);
810 ahci_complete_init(ad_infos + a);
811 }
812 init_complete = 1;
813
814 /* DAZ turn off COM port output if on */
815 //com_base = 0;
816
817 /* release all adapters */
818 for (a = 0; a < ad_info_cnt; a++) {
819 unlock_adapter(ad_infos + a);
820 }
821
822 /* register APM hook */
823 apm_init();
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 printf("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 spin_lock(drv_lock);
1405 while (ai->busy) {
1406 spin_unlock(drv_lock);
1407 msleep(250);
1408 spin_lock(drv_lock);
1409 }
1410 ai->busy = 1;
1411 spin_unlock(drv_lock);
1412}
1413
1414/******************************************************************************
1415 * Unlock adapter (i.e. reset busy flag)
1416 */
1417void unlock_adapter(AD_INFO *ai)
1418{
1419 ai->busy = 0;
1420}
1421
1422/******************************************************************************
1423 * Timeout handler for I/O commands. Since timeout handling can involve
1424 * lengthy operations like port resets, the main code is located in a
1425 * separate function which is invoked via a context hook.
1426 */
1427void _cdecl _far timeout_callback(ULONG timer_handle, ULONG p1,
1428 ULONG p2)
1429{
1430 IORBH _far *iorb = (IORBH _far *) p1;
1431 int a = iorb_unit_adapter(iorb);
1432 int p = iorb_unit_port(iorb);
1433
1434 ADD_CancelTimer(timer_handle);
1435 dprintf("timeout for IORB %Fp\n", iorb);
1436
1437 /* Move the timed-out IORB to the abort queue. Since it's possible that the
1438 * IORB has completed after the timeout has expired but before we got to
1439 * this line of code, we'll check the return code of iorb_queue_del(): If it
1440 * returns an error, the IORB must have completed a few microseconds ago and
1441 * there is no timeout.
1442 */
1443 spin_lock(drv_lock);
1444 if (iorb_queue_del(&ad_infos[a].ports[p].iorb_queue, iorb) == 0) {
1445 iorb_queue_add(&abort_queue, iorb);
1446 iorb->ErrorCode = IOERR_ADAPTER_TIMEOUT;
1447 }
1448 spin_unlock(drv_lock);
1449
1450 /* Trigger abort processing function. We don't really care whether this
1451 * succeeds because the only reason why it would fail should be multiple
1452 * calls to DevHelp_ArmCtxHook() before the context hook had a chance to
1453 * start executing, which leaves two scenarios:
1454 *
1455 * - We succeded in arming the context hook. Fine.
1456 *
1457 * - We armed the context hook a second time before it had a chance to
1458 * start executing. In this case, the already scheduled context hook
1459 * will process our IORB as well.
1460 */
1461 DevHelp_ArmCtxHook(0, reset_ctxhook_h);
1462
1463 /* Set up a watchdog timer which calls the context hook manually in case
1464 * some kernel thread is looping around the IORB_COMPLETE status bit
1465 * without yielding the CPU (kernel threads don't preempt). This shouldn't
1466 * happen per design because kernel threads are supposed to yield but it
1467 * does in the early boot phase.
1468 */
1469 ADD_StartTimerMS(&th_reset_watchdog, 5000, (PFN) reset_watchdog, 0, 0);
1470}
1471
1472/******************************************************************************
1473 * Reset handler watchdog. If a timeout occurs, a context hook is armed which
1474 * will execute as soon as a kernel thread yields the CPU. However, some
1475 * kernel components won't yield the CPU during the early boot phase and the
1476 * only way to kick some sense into those components is to run the context
1477 * hook right inside this timer callback. Not exactly pretty, especially
1478 * considering the fact that context hooks were implemented to prevent running
1479 * lengthy operations like a port reset at interrupt time, but without this
1480 * watchdog mechanism we run the risk of getting completely stalled by device
1481 * problems during the early boot phase.
1482 */
1483void _cdecl _far reset_watchdog(ULONG timer_handle, ULONG p1,
1484 ULONG p2)
1485{
1486 /* reset watchdog timer */
1487 ADD_CancelTimer(timer_handle);
1488 dprintf("reset watchdog invoked\n");
1489
1490 /* call context hook manually */
1491 reset_ctxhook(0);
1492}
1493
1494/******************************************************************************
1495 * small_code_ - this dummy func resolves the undefined reference linker
1496 * error that occurrs when linking WATCOM objects with DDK's link.exe
1497 */
1498void _cdecl small_code_(void)
1499{
1500}
1501
1502/******************************************************************************
1503 * Add unit info to ADAPTERINFO array (IOCC_GET_DEVICE_TABLE requests). The
1504 * adapter info array in the device table, dt->pAdapter[], is expected to be
1505 * initialized for the specified index (dt_ai).
1506 *
1507 * Please note that the device table adapter index, dta, is not always equal
1508 * to the physical adapter index, a: if SCSI emulation has been activated, the
1509 * last reported adapter is a virtual SCSI adapter and the physical adapter
1510 * indexes for those units are, of course, different from the device table
1511 * index of the virtual SCSI adapter.
1512 */
1513static int add_unit_info(IORB_CONFIGURATION _far *iorb_conf, int dta,
1514 int a, int p, int d, int scsi_id)
1515{
1516 DEVICETABLE _far *dt = iorb_conf->pDeviceTable;
1517 ADAPTERINFO _far *ptr = (ADAPTERINFO _far *) (((u32) dt & 0xffff0000U) +
1518 (u16) dt->pAdapter[dta]);
1519 UNITINFO _far *ui = ptr->UnitInfo + ptr->AdapterUnits;
1520 AD_INFO *ai = ad_infos + a;
1521
1522 if ((u32) (ui + 1) - (u32) dt > iorb_conf->DeviceTableLen) {
1523 dprintf("error: device table provided by DASD too small\n");
1524 iorb_seterr(&iorb_conf->iorbh, IOERR_CMD_SW_RESOURCE);
1525 return(-1);
1526 }
1527
1528 if (ai->ports[p].devs[d].unit_info == NULL) {
1529 /* provide original information about this device (unit) */
1530 memset(ui, 0x00, sizeof(*ui));
1531 ui->AdapterIndex = dta; /* device table adapter index */
1532 ui->UnitHandle = iorb_unit(a, p, d); /* physical adapter index */
1533 ui->UnitIndex = ptr->AdapterUnits;
1534 ui->UnitType = ai->ports[p].devs[d].dev_type;
1535 ui->QueuingCount = ai->ports[p].devs[d].ncq_max;;
1536 if (ai->ports[p].devs[d].removable) {
1537 ui->UnitFlags |= UF_REMOVABLE;
1538 }
1539 if (scsi_id > 0) {
1540 /* set fake SCSI ID for this unit */
1541 ui->UnitSCSITargetID = scsi_id;
1542 }
1543 } else {
1544 /* copy updated device (unit) information (IOCM_CHANGE_UNITINFO) */
1545 memcpy(ui, ai->ports[p].devs[d].unit_info, sizeof(*ui));
1546 }
1547
1548 ptr->AdapterUnits++;
1549 return(0);
1550}
1551
1552/*******************************************************************************
1553 * Register kernel exit handler for trap dumps. Our exit handler will be called
1554 * right before the kernel starts a dump; that's where we reset the controller
1555 * so it supports BIOS int13 I/O calls.
1556 */
1557static void register_krnl_exit(void)
1558{
1559 _asm {
1560 push ds
1561 push es
1562 push bx
1563 push si
1564 push di
1565
1566 mov ax, FLAG_KRNL_EXIT_ADD
1567 mov cx, TYPE_KRNL_EXIT_INT13
1568 mov bx, SEG asm_krnl_exit
1569 mov si, OFFSET asm_krnl_exit
1570 mov dl, DevHlp_RegisterKrnlExit
1571
1572 call dword ptr [Device_Help]
1573
1574 pop di
1575 pop si
1576 pop bx
1577 pop es
1578 pop ds
1579 }
1580
1581 dprintf("Registered kernel exit routine for INT13 mode\n");
1582}
1583
Note: See TracBrowser for help on using the repository browser.