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

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

Makefile updates
Debug output improvements
Support for ACPI suspend/resume added

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