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

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