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

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