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

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