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

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