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

Last change on this file since 125 was 125, checked in by cjm, 14 years ago

Version 1.21


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