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

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

Minor change for makefile dependencies

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