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

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