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

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

Begin adding user info output.
Added LVM support.

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